Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7525993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:42:37+00:00 2026-05-30T03:42:37+00:00

I’ve written the following function which imports .txt files into MATLAB . The .txt

  • 0

I’ve written the following function which imports .txt files into MATLAB. The .txt files can either be recorded at hourly intervals or 4 minute intervals where, depending on the initial resolution, the script will calculate the hourly or daily averages.

function [Daily, Hourly] = calc_avg(pathName)
    TopFolder = pathName;
    dirListing = dir(fullfile(TopFolder,'*.txt'));%#Lists the folders in the directory
                                                  %#specified by pathName.
    for i = 1:length(dirListing);
        fileToRead1{i} = (dirListing(i).name);
        %#lists all of the .txt files in the TopFolder
    end
    cell_all = arrayfun(@(i1)importdata(fullfile(pathName,dirListing(i1).name)),...
        (1:length(dirListing))','un',0); %#'

    %# Apply function to each element of the array.
    d = cat(2,cell_all{:});

    %# Concatenate arrays along each column (i.e. 2).
    %# Find the length of the dataset, which will provide information on the
    %# amount of averaging required.
    if length(d) == 365,...
            error('error: daily averages already calculated'); %#'
    elseif length(d) == 8760;
        daily = squeeze(mean(reshape(d,24,size(d,1)/24,[])));
    elseif length(d) == 131400;
        hourly = squeeze(mean(reshape(d,15,size(d,1)/15,[])));
        daily = squeeze(mean(reshape(d,360,size(d,1)/360,[])));
    end

    %# Find which averages have been calculated:
    A = exist('hourly','var');

    %# If A == 1 means that hourly values had to be calculated therefore
    %# the data if of high resolution (minutes).
    if A == 1;
        hourly = mat2cell(hourly,size(hourly,1),cellfun('size',cell_all,2)).'; %#'
        daily = mat2cell(daily,size(daily,1),cellfun('size',cell_all,2)).'; %#'
    elseif A == 0;
        daily = mat2cell(daily,size(daily,1),cellfun('size',cell_all,2)).';%#'
    end

    %# Create cell in the same format as 'cell_all' where cellfun applies the
    %# same function to each cell in a cell array. 'size' is used to create
    %# the same format.
    for i=1:length(dirListing);
        [~,name{i}] = fileparts(fileToRead1{i});
        %# Obtain the name of each of the .txt files (dirListing)
    end

    %#Generate a structure for the averages calculated.
    if A == 1;
        for i=1:length(dirListing);
            Daily.(genvarname(name{i})) = daily{i};
            Hourly.(genvarname(name{i})) = hourly{i};
        end
    elseif A == 0;
        for i=1:length(dirListing);
            Daily.(genvarname(name{i})) = daily{i};
        end
end

The script works fine if I simply run it as a script i.e. avoid using the function and just type the path Name into the second line. But once I try and use if as a function it fails to work. It generates an error:

Error in calc_avg (line 15)
TopFolder = pathName;

What am I doing wrong? Does the problem arise because pathName is a string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:42:39+00:00Added an answer on May 30, 2026 at 3:42 am

    You have to save the function in a separate file called calc_avg.m (in a current folder or folder in the MATLAB path) and run it from a separate script or command line as

    pathName = 'path/to/file';
    [Daily, Hourly] = calc_avg(pathName)
    

    You probably get the error because you are trying to run the function as a script within the editor with Run (f5).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.