Matlab loading data and plotting help

In summary, you are trying to load multiple sets of data into a loop, but you are not able to do this. You need to set up a path for the files, and then use textscan to load the data into variables.
  • #1
blizzard12345
14
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
  • #2
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
 
  • #3
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'
 
  • #4
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.
 
  • #5
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
 

What is Matlab and how can it help with loading data and plotting?

Matlab is a programming language and software environment commonly used in scientific and engineering fields. It has powerful features for loading, processing, and visualizing data, making it a useful tool for data analysis and plotting.

How do I load data into Matlab?

To load data into Matlab, you can use the load function. This function can read data from various file formats, such as text, Excel, or HDF5, and store it in a variable in the workspace. You can also use the importdata function to load data from text files with specific formats.

What is the difference between the plot and scatter functions in Matlab?

The plot function in Matlab creates a line plot, where each data point is connected by a line. On the other hand, the scatter function creates a scatter plot, where each data point is represented by a marker. Scatter plots are useful for visualizing the relationship between two variables, while line plots are better for showing trends over time or between data points.

Can I customize the appearance of my plot in Matlab?

Yes, you can customize various aspects of your plot in Matlab, such as the axes, labels, colors, and markers. You can use functions like xlabel, ylabel, and title to add labels to your plot, and set to change other properties. Additionally, you can use the subplot function to create multiple plots in one figure.

How can I save my plot in Matlab?

To save your plot in Matlab, you can use the saveas function. This function allows you to specify the file format, such as PNG or PDF, and the resolution of your saved plot. You can also use the print function to save your plot directly to a printer or print it to a file.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
928
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Back
Top