Trouble reading an input file and plotting the file's data
Afficher commentaires plus anciens
Hello,
I am attempting to read an input file, and plot the file's data. At first glance, my question seems like a simple fix... however it really isn't that easy.
I started off with the following code:
clear,clc;
load atmospheretable.dat
y = atmospheretable(:,1); x = atmospheretable(:,3);
plot(x,y);
xlabel('temperature'); ylabel('altitude');
title('temperature vs. altitude')
which is a very straight-forward way to read a file and plot the data. This method worked perfectly. However, I do not think that a filename should ever be hard-wired into code, so I modified the code to allow the user to select a file. My code became as such:
clear, clc;
filename = uigetfile('*.*', 'Select File');
load(filename);
y = filename(:,1); x = filename(:,3);
plot(y,x);
xlabel('temperature'); ylabel('altitude');
title('temperature vs altitude');
I subsequently get: ??? Error using ==> plot Invalid first data argument
Error in ==> atmosphereplot at 24 plot(y,x);
I have no idea why i am getting this error! This should work! Could somebody explain to me what I'm doing wrong here? I've been going at this for three hours now with no resolution!
Thank you!
Réponse acceptée
Plus de réponses (1)
Jiro Doke
le 30 Jan 2011
Assuming that your file is an ASCII file with tabulated numbers (like this):
0 0.1661
0.0100 0.1761
0.0200 0.1861
0.0300 0.1961
0.0400 0.2061
0.0500 0.2161
0.0600 0.2261
using the load command will load the data in with the name of the file as the variable name. You should instead use an output argument to the load command, like this:
filename = uigetfile('*.*', 'Select File');
xy = load(filename);
plot(xy(:, 1), xy(:, 2))
Take a look at the documentation for load for more information on this syntax. It behaves differently if you are loading from a MAT file, but for tabular ASCII file, the above syntax should work.
Catégories
En savoir plus sur App Building dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!