MATLAB Matlab loading data and plotting help

AI Thread Summary
The discussion revolves around loading multiple datasets in a loop and plotting them in MATLAB. The initial code attempts to load variables from files named in a specific pattern, but the user encounters issues with loading the data correctly. A suggestion is made to use `fopen` to read the files, but the user experiences a problem where `fopen` returns -1, indicating that the file cannot be found. This prompts a discussion about ensuring the correct file format and path. Ultimately, the user resolves the issue by modifying the code to access the variables correctly using the syntax "variable.int_force" for plotting, indicating a successful adjustment to the original approach.
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
 

Similar threads

Replies
8
Views
2K
Replies
4
Views
1K
Replies
4
Views
3K
Replies
3
Views
7K
Replies
12
Views
3K
Replies
9
Views
5K
Replies
4
Views
2K
Back
Top