How do I change variable names without getting matrix dimension error?

조회 수: 3 (최근 30일)
Martin Matisoff
Martin Matisoff 2017년 4월 6일
편집: Walter Roberson 2017년 4월 6일
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]
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

답변 (2개)

Walter Roberson
Walter Roberson 2017년 4월 6일
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.
  댓글 수: 2
Martin Matisoff
Martin Matisoff 2017년 4월 6일
편집: Walter Roberson 2017년 4월 6일
I think I muddied the water.
C = textscan is the same in both programs. I just forgot to include it in the modified program.
Here is my actual problem.
When I change the variable names from s, t, u etc. to their actual variable names, i.e., s=Ra, t=Rs, u=RH, v=Rn w=G x=Ta y=Ts and z=U2, I get an error when it reaches the FOR loop.
Here is what I have that works:
s = 0; %Rain
t = 0; %Rs
u = 0; %RH
v = 0; %Rn
w = 0; %G
x = 0; %Ta
y = 0; %Ts
z = 0; %Wind
for i = 1:n
s = s + Rain(i)
t = t + Rs(i);
u = u + RH(i);
v = v + Rn(i);
w = w + G(i);
x = x + Ta(i);
y = y + Ts(i);
z = z + Wind(i);
end
Here is the problematic code:
Ra = 0; %Rain
Rs = 0; %Rs
RH = 0; %RH
Rn = 0; %Rn
G = 0; %G
Ta = 0; %Ta
Ts = 0; %Ts
U2 = 0; %Wind
For i = 1:n
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);

댓글을 달려면 로그인하십시오.


Jan
Jan 2017년 4월 6일
편집: Jan 2017년 4월 6일
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);
  댓글 수: 1
Martin Matisoff
Martin Matisoff 2017년 4월 6일
I am trying to calculate the sums based on date of year, i.e., sum all records where DOY = 260, then all records where DOY = 261, and then DOY = 262. I was using the FOR LOOP so that I could limit the sums to each DOY. Hope that makes sense.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Instrument Control Toolbox Supported Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by