Clear Filters
Clear Filters

How can i change file within a loop?

11 views (last 30 days)
Ellen on 20 Feb 2024
Edited: Jon on 27 Feb 2024
%two snippets of code
gauge.source='PSMSL';'Vlissingen';'20';'Hoek van Holland';'22';'Roompot Buiten';'470';'Zeebrugge';'1551';
%snippet 2
filename = 'psmsl_monthly_20.txt';
% Open the file and read all lines to determine the number of lines
fileID = fopen(filename, 'r');
v = textscan(fileID, '%s%*[^\n]');
So far everything runs But i want to change the filename voor more datat to read Ihav tried with numstring but I cannor figure out what I do wrong. Now my code runs but de data from Vlissingen are used for the other stations to. (ID 20)
How can i replace the code
filename = 'psmsl_monthly_20.txt';
to something like filename = 'psmsl_monthly_ "next station id".txt'; and let thecode read all the files.

Accepted Answer

Jon on 20 Feb 2024
I think you are looking for something like this
for k = 1:12
filename = ['psmsl_monthly_',num2str(k),'.txt']
% do something with filename, etc.
filename = 'psmsl_monthly_1.txt'
filename = 'psmsl_monthly_2.txt'
filename = 'psmsl_monthly_3.txt'
filename = 'psmsl_monthly_4.txt'
filename = 'psmsl_monthly_5.txt'
filename = 'psmsl_monthly_6.txt'
filename = 'psmsl_monthly_7.txt'
filename = 'psmsl_monthly_8.txt'
filename = 'psmsl_monthly_9.txt'
filename = 'psmsl_monthly_10.txt'
filename = 'psmsl_monthly_11.txt'
filename = 'psmsl_monthly_12.txt'
Ellen on 25 Feb 2024
nd_yr=find(year==jj); % select entries with year jj;
you mean I have tot change jj (above txt) to jjjj?
nd_yr=find(year==jjjj); % select entries with year jj;
Jon on 27 Feb 2024
Edited: Jon on 27 Feb 2024
I think you are getting confused about working with character vectors, e.g. '1996' and numerical values, 1996.
I'm not totally clear on what you are suggesting here, but it somehow looks like you are thinking that you should compare to jjjj rather than jj to get more digits. If so, then this is not what I meant at all.
Here is the situation.
You first assign a numerical values, (not a character vectors or strings) to your variables date and year, on the lines where you have
date=alldata(:,1); % in decimal years
If you look at the values for alldata(:,1), the column you are assigning to date, it is a vector of numerical values (not characters or strings) whose elements are 0, 1, 2, .... 100.
As they do not have any decimal fraction, taking the floor of them leaves them unchanged, so your variable year, is a vector of numerical values (not characters or strings) whose elements are 0,1,2,...100
Next you have a loop in which you assign the loop variable jj, to take on numerical values (not strings or characters) from startyear to endyear which you assigned earlier using
% select years:
%startyear=1995; % for plot
so as you loop jj wil take on the numerical values 1996,1997,1998,...2022
Inside of the loop you look for the ind_yr (index into the vector of numerical year values) using
ind_yr=find(year==jj); % select entries with year jj
so you are looking for the element of year where year has a value that matches jj.
Since the elements of year have values, 0,1,2,...101 and, lets say on the first loop, jj = 1996, it clearly will not find any match, there is no element of year that has the value 1996, so it returns empty.
Next you try to compute the mean value of the level corresponding to the ind_yr with right hand side of the line;
Since ind_yr is empty, level(ind_yr) is also empty. Since MATLAB doesn't know how to compute the mean of an empty vector, it simple return NaN (which means not a number), and NaN is assigned to
This continues with each successive loop, on the next loop jj =1997, which also is not found in year, which only has value 0,1,2,...101. and again the ind_yr is empty the mean level is NaN until finally all of the values of have been assigned NaN
After that there are no useful results, as you try to compute correlations etc from these vectors of NaN and obviously it can't compute anything useful just using NaN's, and eventually you actually have an error.
So you need to either get your year values to be in the range 1996 to 2022, perhaps by adding your variable called date to some base year. Alternatively you leave the year values as 0,1,2,... and modify the condition you check for a match, so if the year values were, for example, the number of years after 1990, then you could check for
ind_yr = find(year == jj - 1990)
so when, for example jj = 1999, it would give ind_yr = 10, since year(10) has a value of 9 (your year values start at zero) and 1999-1990 equals 9
I hope this helps.

Sign in to comment.

More Answers (0)


Find more on Loops and Conditional Statements in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!