Matlab loading data and plotting help

  • Context: MATLAB 
  • Thread starter Thread starter blizzard12345
  • Start date Start date
  • Tags Tags
    Data Matlab Plotting
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 4K views
blizzard12345
Messages
11
Reaction score
0
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 can't 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
 
Physics news on Phys.org
Your code doesn't seem to be doing exactly as you're saying. The method I use which works with 100% certainty is by doing this:

Code:
%Begin Loop
intforce_str = strcat('internalforce_', num2str(i),'.txt'); %I assume its a txt file
file_name = fopen(intforce_str,'r')

[I]%this line depends on your data format. 
%I like to place the data in a format where textscan simply reads 
%your data column by column and places it in a cell corresponding 
%to each column. You don't have to do this, 
%but this is most intuitive to me.[/I]
variable = textscan(file_name,'%g') 

defl_str = strcat('internalforce_', num2str(i));
file2_name = fopen(defl_str,'r')

%same as above
variabletwo = textscan(file2_name,'%g') 

plot(variable,variabletwo)
hold on

end %end plot loop

%I usually turn hold off by instinct.
hold off

end %end load_data
 
hi thanks for the reply when i use fopen it returns a value of -1 which I've read to mean it can't read my data?.. does this mean I am saving the data in the wrong format? I am currently using the below code to save a column vector

fn = sprintf('deflection_%s',num2str(ii));
save(fn,'deflection')

along with a loop to determine the number 'ii'
 
If fopen returns -1 that means your current path is not set to the correct folder or the file does not exist. try using the full path of the file instead.
 
hi i made the original code work now, i just needed to plot the variable and variabletwo in the form "variable.int_force" so use the saved variable name after it thanks for your help