How to assign variable names and import data via loop?
3 views (last 30 days)
Show older comments
Thomas Kelly
on 23 Mar 2017
Commented: Star Strider
on 25 Mar 2017
Hi all,
I have recently completed an assignment where I was required to plot graphs of various soil tests. The data from the tests was stored in .csv files, I've imported the files using xlsread but for each test, I simply copy and pasted the code to import the data. This was OK for the purposes of this assignment, but how can I make it more efficient for future assignments? An example of how have done the assignment is as follows:
%%Data import
%Test 1 10kg
t1 = xlsread('group21.csv', 'A4:A1216')'; % Seconds
t1(find(t1>200)) = NaN;
L1 = abs(xlsread('group21.csv', 'B4:B1216'))'; % Newtons
s1=L1./(0.1*0.1); % Pascals
d1= xlsread('group21.csv', 'C4:C1216')'; % Micro Meters
This was repeated (copy and pasted) for FIVE tests. How can I write it in a loop such that I can get it to output t1,t2,....,L1,L2... etc by simply typing t1 in the command window? I want it to just display that variable t1.
Further more I then plotted these as follows:
figure('Color', [1 1 1])
hold on
grid on
grid minor
plot(t1.*(1/30),s1,'.r')
plot(t2.*(1/30),s2, '.b')
plot(t3.*(1/30),s3, '.g')
plot(t4.*(1/30),s4, '.m')
plot(t5.*(1/30),s5, '.k')
How could I automate it so I can type plot(something here to make it plot all t1 t2 etc, something here to plot s1 s2 etc) ?
Any assistance would be appreciated as I feel copying and pasting the code for each test is beginning to create more problems than it solves as I conduct more complicated tests as my degree progresses.
Thanks in advance.
0 Comments
Accepted Answer
Star Strider
on 24 Mar 2017
A loop with a cell array would likely work:
for k1 = 1:5
t{k1} = xlsread('group21.csv', 'A4:A1216')'; % Seconds
t{k1}(t{k1}>200) = NaN;
L{k1} = abs(xlsread('group21.csv', 'B4:B1216'))'; % Newtons
s{k1}=L{k1}./(0.1*0.1); % Pascals
d{k1}= xlsread('group21.csv', 'C4:C1216')'; % Micro Meters
end
Note — I can test part of this but not all of it, so I am labeling it UNTESTED CODE. It should work, but without your Excel files, I can’t test it.
2 Comments
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!