Is it possible to use the for-loop for this purpose?
이전 댓글 표시
Although now I'm doing this work manually by my hand if there is any chance to know I want to ask is there any way to do this using for loop?
I want to polyfit two by two based on a rule and know slope and y-intercept for each one that runs.
I have a table that describes the name of a couple of station names that should be used in polyfit together. on the other hand, I have a cell including monthly tables for these stations data.
I wonder if any way available to do this like in the code below automatically: (In the table that describes the name of couple station name that should be used in polyfit together there is Ahvaz and Ramhormoz is beside each other)
%calculate P for January (thanks to Adam Danz)
Ahvaz_Jan = getfield(finalStations,'Ahvaz_Jan');
Ramhormoz_Jan = getfield(finalStations,'Ramhormoz_Jan');
nanIdx = isnan(Ahvaz_Jan.tmax_m) | isnan(Ramhormoz_Jan.tmax_m);
P = polyfit(Ahvaz_Jan.tmax_m(~nanIdx), Ramhormoz_Jan.tmax_m(~nanIdx), 1); % January P (slope and Y intercept)
% calculate P for Feburary
Ahvaz_Feb = getfield(finalStations,'Ahvaz_Feb');
Ramhormoz_Feb = getfield(finalStations,'Ramhormoz_Feb');
nanIdx = isnan(Ahvaz_Feb.tmax_m) | isnan(Ramhormoz_Feb.tmax_m);
P = polyfit(Ahvaz_Feb.tmax_m(~nanIdx), Ramhormoz_Feb.tmax_m(~nanIdx), 1); % Feburary P (slope and Y intercept)
% For March ...
% ...For December
% looking for next two stations name from table_of_couple_stations.xlsx and do above steps for them
and then looking at the table that describes the name of a couple of station names that should be used in polyfit together and find the next couple and do this for it, and repeat this process until the end, and gives me a table of all P with corresponding stations name.
I don't know if this could be done in Matlab but if there is any way it's a fabulous moment for me.
댓글 수: 2
Adam Danz
2020년 1월 27일
I didn't recommend using getfield.
Ahvaz_Jan = getfield(finalStations,'Ahvaz_Jan');
Ramhormoz_Jan = getfield(finalStations,'Ramhormoz_Jan');
That can be replaced by
Ahvaz_Jan = finalStations.Ahvaz_Jan; % or finalStations(1).Ahvaz_Jan
Ramhormoz_Jan = finalStations.Ramhormoz_Jan; % or finalStations(1).Ramhormoz_Jan
That's described in the first line of text in getfield().
Adam Danz
2020년 1월 27일
Now you have a structure with 852 uniquely named fields, each containing a table for a station and a month. If you want to access a specific field such as the Ahvaz station for the month of January ("Ahvaz_Jan"), you'll have to construct the field name strings like this.
fieldString = sprintf('%s_%s','Ahvaz','Jan');
T = finalStations.(fieldString);
I think the cell array approach in that question/answer would be easier to work with (I'm biased because I wrote that answer). With that approach, you have a nx12 cell array of n-stations and 12 months. You also have a 1xn cell array of station names. So identifying the table would look like this.
T = Cmo{strcmp(stationList,'Ahvaz'), 1}
% Where "stationList" is your cell array of station names
% and the 1 is for January.
채택된 답변
추가 답변 (1개)
Mohammad Sami
2020년 1월 27일
편집: Mohammad Sami
2020년 1월 27일
You can use the for loop. Modify it as you deem fit.
% load from xlsx or from your mat file.
matching_station = readtable('table_of_couple_stations.xlsx');
monthnames = {'Jan' 'Feb' 'Mar' 'Apr'} % and the rest of names
for i = 1:height(matching_station);
for j = 1:length(monthnames)
nameA = [matching_station.station_name{i} '_' monthnames{j}]; % {i} assumed its a cell array
nameB = [matching_station.closest_station{i} '_' monthnames{j}];
tempA = getfield(finalStations,nameA);
tempB = getfield(finalStations,nameB);
nanIdx = isnan(tempA.tmax_m) | isnan(tempB.tmax_m);
P = polyfit(tempA.tmax_m(~nanIdx), tempB.tmax_m(~nanIdx), 1);
end
end
댓글 수: 3
The stations names in table_of_couple_stations do not match the field names, though. For example, "'Tehran (Mehrabad Airport)'" cannot be a field name.
This is another problem using the structure approach that wouldn't be a problem with the nx12 cell array approach.
BN
2020년 1월 27일
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!