How do I change variable names without getting matrix dimension error?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
The following code works as is, but when I try to rename the variables, e.g., s -> Ra, t -> Rs u -> RH, etc. I get an "exceeds matrix dimensions" error. My changed code is listed after the "This Works" area. The section I show as "Problem area" is when the errors occur. I have shown the changed code after the following code (shown below "&&&&&&&&" line). I hope this makes sense.
****************THIS WORKS ****************
filename = 'E:\My Documents\Maheteme\Sample Programs\BiometDataSetTestData_2a.csv';
delimiter = ',';
startRow = 2; %assign startRow to 'Q', first line of data
formatSpec = '%f%f%f%f%f%f%f%f%f%s%s%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
fclose(fileID);
d = dataArray{:, 1}; %DOY
Ra = dataArray{:, 2};
Rs = dataArray{:, 3};
RH = dataArray{:, 4};
Rn = dataArray{:, 5};
G = dataArray{:, 6};
Ta = dataArray{:, 7};
Ts = dataArray{:, 8};
U2 = dataArray{:, 9};
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
headerlines = 1;
start_string = '260';
file_name = 'E:\My Documents\Maheteme\Sample Programs\BiometDataSetTestData_2.csv';
fileID = fopen(file_name);
line_no = 1;
n = 118; %Total number of rows
Ra = 0; %Rain
Rs = 0; %Rs
RH = 0; %RH
Rn = 0; %Rn
G = 0; %G
Ta = 0; %Ta
Ts = 0; %Ts
U2 = 0; %Wind
while feof(fileID) == 0
tline{line_no} = fgetl(fileID);
line_no = line_no+1;
end
index = strmatch(start_string,tline(headerlines+1:length(tline)))+headerlines;
S = tline{index(end)}
C = textscan (S, '%f %f %f %f %f %f %f %f %f %f %f');
index = index([end]);
n = index-1;
for i = 1:n %Sum of each column
Ra = Ra + Ra(i);
Rs = Rs + Rs(i);
RH = RH + RH(i);
Rn = Rn + Rn(i);
G = G + G(i);
Ta = Ta + Ta(i);
Ts = Ts + Ts(i);
U2 = U2 + U2(i);
end
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& MODIFIED CODE &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
d = dataArray{:, 1}; %DOY
Rain = dataArray{:, 2};
Rs = dataArray{:, 3};
RH = dataArray{:, 4};
Rn = dataArray{:, 5};
G = dataArray{:, 6};
Ta = dataArray{:, 7};
Ts = dataArray{:, 8};
U2 = dataArray{:, 9};
n = 118; %Total number of rows
Ra = 0; %Rain
Rs = 0; %Rs
RH = 0; %RH
Rn = 0; %Rn
G = 0; %G
Ta = 0; %Ta
Ts = 0; %Ts
U2 = 0; %Wind
---------------------------------Errors occurs here -------------------
Index exceeds matrix dimensions.
Error in LoadFileBiometDataCalculations_GOOD_CODE_PART1_rev_001 (line 113)
Ra = Ra + Ra(i);
-----------------------------------------------------------------------
while feof(fileID) == 0
tline{line_no} = fgetl(fileID);
line_no = line_no+1;
end
index = strmatch(start_string,tline(headerlines+1:length(tline)))+headerlines;
%S = tline{index(1)}
S = tline{index(end)}
C = textscan (S, '%f %f %f %f %f %f %f %f %f %f %f');
index = index([end]);
n = index-1;
for i = 1:n %Sum of each column
Ra = Ra + Rain(i);
Rs = Rs + Rs(i);
RH = RH + RH(i);
Rn = Rn + Rn(i);
G = G + G(i);
Ta = Ta + Ta(i);
Ts = Ts + Ts(i);
U2 = Wi + Wind(i);
end
disp([' Rain Rs RH Rn G Ta Ts Wind'])
[Ra Rs RH Rn G Ta Ts U2
Ra_mean = Ra/n;
Rs_mean = Rs/n;
RH_mean = RH/n;
Rn_mean = Rn/n;
G_mean = G/n;
Ta_mean = Ta/n;
Ts_mean = Ts/n;
U2_mean = U2/n;
disp(['Ra_mean Rs_mean RH_mean Rn_mean G_mean Ta_mean Ts_mean U2_mean'])
[Ra_mean Rs_mean RH_mean Rn_mean G_mean Ta_mean Ts_mean U2_mean]
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
0 commentaires
Réponses (2)
Walter Roberson
le 6 Avr 2017
In the top section, you textscan into a variable and extract information from that variable.
d = dataArray{:, 1}; %DOY
and so on.
In the modified version, you
C = textscan (S, '%f %f %f %f %f %f %f %f %f %f %f');
and then do not use C.
Jan
le 6 Avr 2017
Modifié(e) : Jan
le 6 Avr 2017
This must fail:
Rs = dataArray{:, 3}; % Rs is an array
Rs = 0; % Rs is overwritten by the scalar 0
for i = 1:n
Rs = Rs + Rs(i); % Works in the 1st iteration, but not for i=2
end
This should fail in the code marked as "THIS WORKS" also.
What should this loop produce? Do you want to calculate the sum? Then:
Rs = dataArray{:, 3};
Rs_sum = sum(Rs);
Voir également
Catégories
En savoir plus sur Instrument Control Toolbox Supported Hardware dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!