Matlab loading data and plotting help

  • Context: MATLAB 
  • Thread starter Thread starter blizzard12345
  • Start date Start date
  • Tags Tags
    Data Matlab Plotting
Click For Summary

Discussion Overview

The discussion revolves around loading multiple sets of data in MATLAB and plotting them in a loop. Participants share their code snippets and troubleshoot issues related to file loading and data plotting.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant shares a code snippet for loading data using the 'load' function and plotting it, but encounters issues with variable loading.
  • Another participant suggests an alternative method using 'fopen' and 'textscan' for reading data from text files, emphasizing the importance of data format.
  • A participant reports an error when using 'fopen', indicating it returns -1, which suggests a potential issue with file paths or file existence.
  • Another participant advises checking the current path and suggests using the full path to resolve the file loading issue.
  • A participant later reports success in making their original code work by adjusting how they reference saved variables in the plot command.

Areas of Agreement / Disagreement

Participants express differing methods for loading data and plotting, with no consensus on a single approach. Issues regarding file paths and data formats remain unresolved.

Contextual Notes

Participants mention specific file formats and loading methods, but there are unresolved assumptions about the data structure and the environment setup that could affect the outcomes.

Who May Find This Useful

Individuals working with MATLAB for data analysis and plotting, particularly those facing challenges with file I/O operations and data visualization techniques.

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 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K