How can i change file within a loop?
7 次查看(过去 30 天)
显示 更早的评论
%two snippets of code
----------------------------------------------------------------
gauge.source='PSMSL';
gauge.data(1).location='Vlissingen'; gauge.data(1).id='20';
gauge.data(2).location='Hoek van Holland'; gauge.data(2).id='22';
gauge.data(3).location='Roompot Buiten'; gauge.data(3).id='470';
gauge.data(4).location='Zeebrugge'; gauge.data(4).id='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]');
fclose(fileID);
------------------------------------------------------
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.
Ellen
0 个评论
采纳的回答
Jon
2024-2-20
I think you are looking for something like this
for k = 1:12
filename = ['psmsl_monthly_',num2str(k),'.txt']
% do something with filename, etc.
end
11 个评论
Jon
2024-2-27
编辑:Jon
2024-2-27
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
year=floor(date);
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=1996;
%startyear=1995; % for plot
endyear=2022;
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
gauge.data(stat).annual_level(jj-startyear+1)=mean(level(ind_yr));
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
gauge.data(stat).annual_level(jj-startyear+1)
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 guage.data(stat).annual_level 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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!