unable to plot

2 views (last 30 days)
kyle lyth
kyle lyth on 6 May 2011
hi i want to load multiple sets of data in a loop and the plot a linear graph each time the loop runs below is my code so far, i cant seem to load any variable from the file names, the variable are for example int_force=[1;2;3;4;5] deflection=[1;2;3;4;5]
function load_data
clear all
clc
%determins the number of saved files in directory
graph = dir('internalforce_*');
k = size(graph);
%starts a loop to load increase values of eg, internalforce_1,
%internalforce_2...
for i = 1 : k(1)
file_name = sprintf('internalforce_%d',i)
variable = load(file_name,'int_force')
file2_name = sprintf('deflection_%d',i)
variabletwo = load(file2_name,'deflection')
plot(variable,variabletwo)
hold on
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 6 May 2011
You can only specify a variable name to load() if you are loading from a .mat file, and in that case, it would always be a structure array that would be returned, thus requiring
plot(variable.int_force, variabletwo.deflection)
If the files are plain text then you must omit the variable name from the load(), but can otherwise use your code as-is.
  1 Comment
kyle lyth
kyle lyth on 7 May 2011
thank you adding the . extensions makes sense and works great :D

Sign in to comment.

More Answers (1)

Paulo Silva
Paulo Silva on 6 May 2011
If MATLAB doesn't throw out errors when running that code that's because you are loading something correctly, please look at the workspace variables and the command line outputs, also never use the hold on command inside the loop, you just need to use it once before the loop and after creating the axes.
axes
hold on

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!