How can I resolve Error Using max function

clc
clear all;
close all;
for kk =1:44
for k=1:50
% here drift values are in the form of drift ratio
eval(['load IDAOutput/EQ_',num2str(kk),'/Scale_',num2str(k),'/StoryDrifts/','Roof.txt']);
Roof_Drift_max(kk,k,:)= max(abs(Roof(:,2)));
eval(['load IDAOutput/EQ_',num2str(kk),'/Scale_',num2str(k),'/StoryDrifts/','Story1.txt']);
Story1_Drift_max(kk,k,:)= max(abs(Story1(:,2)));
eval(['load IDAOutput/EQ_',num2str(kk),'/Scale_',num2str(k),'/StoryDrifts/','Story2.txt']);
Story2_Drift_max(kk,k,:)= max(abs(Story2(:,2)));
eval(['load IDAOutput/EQ_',num2str(kk),'/Scale_',num2str(k),'/StoryDrifts/','Story3.txt']);
Story3_Drift_max(kk,k,:)= max(abs(Story3(:,2)));
eval(['load IDAOutput/EQ_',num2str(kk),'/Scale_',num2str(k),'/StoryDrifts/','Story4.txt']);
Story4_Drift_max(kk,k,:)= max(abs(Story4(:,2)));
eval(['load IDAOutput/EQ_',num2str(kk),'/Scale_',num2str(k),'/StoryDrifts/','Story5.txt']);
Story5_Drift_max(kk,k,:)= max(abs(Story5(:,2)));
eval(['load IDAOutput/EQ_',num2str(kk),'/Scale_',num2str(k),'/StoryDrifts/','Story6.txt']);
Story6_Drift_max(kk,k,:)= max(abs(Story6(:,2)));
eval(['load IDAOutput/EQ_',num2str(kk),'/Scale_',num2str(k),'/StoryDrifts/','Story7.txt']);
Story7_Drift_max(kk,k,:)= max(abs(Story7(:,2)));
eval(['load IDAOutput/EQ_',num2str(kk),'/Scale_',num2str(k),'/StoryDrifts/','Story8.txt']);
Story8_Drift_max(kk,k,:)= max(abs(Story8(:,2)));
eval(['load IDAOutput/EQ_',num2str(kk),'/Scale_',num2str(k),'/StoryDrifts/','Story9.txt']);
Story9_Drift_max(kk,k,:)= max(abs(Story9(:,2)));
% Maximum Interstorey Drift Ratio
MIDR(kk,k,:) =max(Roof_Drift_max(kk,k,:),Story1_Drift_max(kk,k,:),Story2_Drift_max(kk,k,:),Story3_Drift_max(kk,k,:),Story4_Drift_max(kk,k,:),...
Story5_Drift_max(kk,k,:),Story6_Drift_max(kk,k,:),Story7_Drift_max(kk,k,:),Story8_Drift_max(kk,k,:),Story9_Drift_max(kk,k,:));
end
end

 採用された回答

Star Strider
Star Strider 2021 年 1 月 8 日

0 投票

I am not certain what you want.
One option is to enclose all the arguments within square brackets [], effectively concatenating them:
MIDR(kk,k,:) =max([Roof_Drift_max(kk,k,:),Story1_Drift_max(kk,k,:),Story2_Drift_max(kk,k,:),Story3_Drift_max(kk,k,:),Story4_Drift_max(kk,k,:),...
Story5_Drift_max(kk,k,:),Story6_Drift_max(kk,k,:),Story7_Drift_max(kk,k,:),Story8_Drift_max(kk,k,:),Story9_Drift_max(kk,k,:)]);
This will produce an ‘MIDR’ matrix with the same dimensions as the argument matrices.
Experiment with other approaches to get different results.

16 件のコメント

Sumit Saha
Sumit Saha 2021 年 1 月 8 日
thanks a lot
Star Strider
Star Strider 2021 年 1 月 8 日
As always, my pleasure!
Sumit Saha
Sumit Saha 2021 年 1 月 9 日
@Star Strider I've just tried this one. It's working fine upto 3 but for higher than 3 it's showing some issues
for p=1:5
for q= 1:3
eval(['load IDAOutput\EQ_',num2str(p),'\Scale_',num2str(q),'\NodeDisplacements\','NodeDisplacementLevel1.txt']);
eval(['load IDAOutput\EQ_',num2str(p),'\Scale_',num2str(q),'\NodeDisplacements\','NodeDisplacementLevel2.txt']);
eval(['load IDAOutput\EQ_',num2str(p),'\Scale_',num2str(q),'\NodeDisplacements\','NodeDisplacementLevel3.txt']);
Story1_Drift(:,q,p)= [(NodeDisplacementLevel2(:,2))-(NodeDisplacementLevel1(:,2))] ;
Story1_Drift_max(:,q,p)= max(abs(Story1_Drift(:,q,p)));
% Story2_Drift(p,q,:)= NodeDisplacementLevel3(:,2)-NodeDisplacementLevel2(:,2) ;
% Story3_Drift(p,q,:)= NodeDisplacementLevel4(:,2)-NodeDisplacementLevel3(:,2) ;
end
end
Star Strider
Star Strider 2021 年 1 月 9 日
First, this is not the same as the max function, and
Second, using eval is definitely not recommended.
If you want to load .mat files (or certain text files), use the function implementation, not the command implementation.
Also, always load into a variable, creating a structure in that variable from which it is then relatively easily get the desired components. See Load List of Variables into Structure Array for details.
Sumit Saha
Sumit Saha 2021 年 1 月 9 日
@Star Strider yeah... I've understood your point but There are 44 earthquake folder and under each earthquake folder there are 50 subfolders for each scale from 1 to 50 and under each scale subfolder there is another subfolder titled 'storydrift'......now from this situation how can i build my code wihtout using eval?
Stephen23
Stephen23 2021 年 1 月 9 日
編集済み: Stephen23 2021 年 1 月 9 日
"now from this situation how can i build my code wihtout using eval?"
Why do you need anti-pattern eval to loop over files and folders?
The basic approaches are to either generate the file/folder names or to read them from the OS using dir. Although you can use eval if you really want to force yourself into writing slow, complex, obfuscated, buggy code that is hard to debug, you really should just use simpler and more efficient approaches, e.g.:
As Star Strider stated, your code would be a lot simpler if you used function syntax rather than command syntax. Your totally inappropriate usage of command syntax is how you forced yourself into unnecessary eval usage.
Rather than using num2str and string concatenation, you should use sprintf and fullfile.
Sumit Saha
Sumit Saha 2021 年 1 月 9 日
@Stephen Cobeldick Can you please write few lines of the code so that it can be easier to understand?
How to use fullfile and sprintf in my case ? You can show just one case
ps: code is at the top
Star Strider
Star Strider 2021 年 1 月 9 日
Note that if you actually execute this example line (without the eval call):
['load IDAOutput\EQ_',num2str(p),'\Scale_',num2str(q),'\NodeDisplacements\','NodeDisplacementLevel1.txt']
the result is:
'load IDAOutput\EQ_1\Scale_2\NodeDisplacements\NodeDisplacementLevel1.txt'
since the square brackets [] are concatenation operators.
See if this does what you want:
p = 1;
q = 2;
r = 3;
filepath = fullfile(sprintf('IDAOutput\\EQ_%d\\Scale_%d\\',p,q), 'NodeDisplacements\', sprintf('NodeDisplacementLevel%d.txt',r))
LD = load(filename)
with ‘filepath’ (or whatever you choose to name it) now being:
filepath =
'IDAOutput\EQ_1\Scale_2\NodeDisplacements\NodeDisplacementLevel3.txt'
and all the file contents appearing in the ‘LD’ structure for you to extract.
If you are not familiar with structure arrays, see Access Data in Structure Array for details on how to work with them
.
Stephen23
Stephen23 2021 年 1 月 9 日
fn = sprintf('NodeDisplacementLevel%d.txt',r);
f1 = sprintf('EQ_%d',kk);
f2 = sprintf('Scale_%d',k);
ff = fullfile('IDAOutput',f1,f2,'NodeDisplacements',fn);
LD = load(ff)
Sumit Saha
Sumit Saha 2021 年 1 月 9 日
@Star Strider Can I select only second column of the NodeDisplacementLevel3.txt during execution of loop?
Sumit Saha
Sumit Saha 2021 年 1 月 9 日
One thing more It's not working properly for for-end loop...like I had used in the earlier case
Sumit Saha
Sumit Saha 2021 年 1 月 9 日
@Stephen Cobeldick
Actually it's not working....for kk=1, k=1, r=1:9 ......5 matrix columns are coming
Sumit Saha
Sumit Saha 2021 年 1 月 9 日
編集済み: Sumit Saha 2021 年 1 月 9 日
How Can I open subfolders read the second column of text file and store that in matrix column wise in?
Star Strider
Star Strider 2021 年 1 月 9 日
Sumit Saha —
Can I select only second column of the NodeDisplacementLevel3.txt during execution of loop?
I have no idea what ‘NodeDisplacementLevel3.txt’ is, or what it contains. If it only contains one variable (presumably a matrix), then this could work:
LD = load(filename);
SecondColumn = LD.SomeVariableMatrix(:,2);
If the variable is called ‘NodeDisplacementLevel3’, then something like this could work:
LD = load(filename);
SecondColumn = LD.NodeDisplacementLevel3(:,2);
Obviously, name the assigned variables something that makes sense in the context of your code.
That is as close as I can come to a reply, given the information available.
Sumit Saha
Sumit Saha 2021 年 1 月 9 日
I've attached a zip file for your reference
Star Strider
Star Strider 2021 年 1 月 9 日
Try this to extract the variables:
uz = unzip('IDAOutput.zip');
k2 = 0;
for k1 = 1:numel(uz)
if ~isdir(uz(k1))
[~,txtname] = fileparts(uz{k1});
k2 = k2+1;
LD{k2,1} = txtname;
LD{k2,2} = load(uz{k1});
end
end
I have to unzip it, so I included tahat as part of my code. Change my code to work with the .zip file or it was created from. My code extracts the contents of the .zip file to a series of cell arrays, with the first element of the cell array being the name of the file it came from, and the second cell array the contents of that file as a double matrix.
So to get the information for the first 5 elements of ‘LD(1)’:
BldgPart_1 = [LD{1,1}]
BldgPrtData_1 = [LD{1,2}(1:5,:)]
produces:
BldgPart_1 =
'Roof'
BldgPrtData_1 =
0.002 6.9378e-08
0.004 6.9378e-08
0.006 6.9378e-08
0.008 6.9378e-08
0.01 6.9378e-08
and for ‘LD(40)’:
BldgPart_40 = [LD{40,1}]
BldgPrtData_40 = [LD{40,2}(1:5,:)]
produces:
BldgPart_40 =
'Story9'
BldgPrtData_40 =
0.002 5.5276e-07
0.004 5.5276e-07
0.006 5.5276e-07
0.008 5.5276e-07
0.01 5.5276e-07
I leave the rest to you.

サインインしてコメントする。

その他の回答 (1 件)

Feng Shui Déco
Feng Shui Déco 2021 年 1 月 9 日

0 投票

Yeah, you have to close your arguments with square bracket.

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

製品

リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by