How to assign variable names and import data via loop?

3 views (last 30 days)
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.

Accepted Answer

Star Strider
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
Thomas  Kelly
Thomas Kelly on 25 Mar 2017
Yeah that works great thanks. Changed it a bit but the idea you presented was pretty much what I wanted. Here is what I have now that works:
%%Import
for k1 = 1:5
t{k1} = xlsread(['group2', num2str(k1) '.csv'], 'A4:A1216')'; % Seconds
t{k1}(t{k1}>200) = NaN;
L{k1} = abs(xlsread(['group2', num2str(k1) '.csv'], 'B4:B1216'))'; % Newtons
s{k1}=L{k1}./(10); % Pascals
%d{k1}= xlsread('group21.csv', 'C4:C1216')'; % Micro Meters
end
%Plot
figure('Color', [1 1 1])
for k1 = 1:5
hold on
grid on
grid minor
plot(t{1,k1}./30,s{1,k1}, '.')
axis square
end
xlabel('Displacement (\delta_x - mm)')
ylabel('Shear Stress (\tau - kPa)')
legend('Test 1 - 10kg', 'Test 2 - 20kg','Test 3 - 30kg','Test 4 - 40kg','Test 5 - 50kg','Location', 'SouthEast')
title('Shear Stress vs Horizontal Displacement')
axis square
hold off
The result is the attached graph.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!