How do you plot sequentially numbered files on one plot?

5 次查看(过去 30 天)
I am new to MATLAB and have created ascii folders with 30 .asc files in each that I would like to plot on one graph. I have tried to figure out how to do this, but have a limited understanding of MATLAB. I know I need to include the matrix size (200:1), but this is actually 2 columns with x,y values in it.
So far all I have is:
Files = ('sample1_1_*.asc');
clf,figure,hold on;
for k = 1:30
plot(Files(k,:))
end
But this gives an error of 'Index exceeds matrix dimensions.'
Any help would be much appreciated or if you could direct me to a relevant post that would be fantastic!
  3 个评论
S B
S B 2017-9-6
It now comes up with the error 'Index exceeds matrix dimensions.'
OCDER
OCDER 2017-9-6
编辑:OCDER 2017-9-6
It's caused because the file name is just a 1-line string in matlab. "Files" does not store the 30 asc file names correctly (hence you'll get an index exceed error). I think I can give you a function that'll work, but could you copy and paste just the first 5 data points for one asc file that you want to plot? Just so I can see if matlab has the right file reader.

请先登录,再进行评论。

采纳的回答

OCDER
OCDER 2017-9-6
编辑:OCDER 2017-9-6
Assuming your asc txt files stores the data in the following pattern:
x y
x y
x y
Save the following functions as plotAscData.m, and run plotAscData
> plotAscData
function plotAscData
%Select multiple files and store the file name into a cell array
[FileName, FilePath] = uigetfile('*.asc', 'Open files', 'multiselect', 'on');
if ischar(FileName)
FileName = {FileName};
elseif isnumeric
warning('No file selected');
return;
end
figure;
hold on;
for f = 1:length(FileName)
FullFileName = [FilePath FileName{f}];
M = readAscFile(FullFileName); %Reads csv file as a matrix
x = M(:, 1); %Assuming x values are the 1st column
y = M(:, 2); %Assuming y values are the 2nd column
plot(x, y);
end
hold off;
function M = readAscFile(AscFileName)
FID = fopen(AscFileName, 'r'); %Access asc file
%Count number of columns, if possible
if feof(FID) == 0
TextLine = fgetl(FID);
NumCol = length(regexpi(TextLine, '\s', 'split'));
else %Nothing in this file
warning('File is empty.');
M = [];
return;
end
fseek(FID, 0, 'bof'); %Return to file beginning
FileStrFormat = repmat('%f', 1, NumCol); %Specify asc data format
ScannedText = textscan(FID, FileStrFormat); %Read the data into a cell array
%Convert the ScannedText cell array into the double array M
M = zeros(length(ScannedText{1}), NumCol); %Preallocate double matrix M
for j = 1:NumCol
M(:, j) = ScannedText{j};
end
fclose(FID); %Close file to free up memory
  5 个评论
S B
S B 2017-9-7
Thanks so much for this! In the end, a combination of the 2 worked for me:
%Select multiple files and store the file name into a cell array
[FileName, FilePath] = uigetfile('*.asc', 'Open files', 'multiselect', 'on');
if ischar(FileName)
FileName = {FileName};
elseif isnumeric(FileName)
error('No file selected');
end
figure;
hold on;
for f = 1:length(FileName)
FullFileName = [FilePath FileName{f}];
M = dlmread(FullFileName);
x = M(:, 1); %Assuming x values are the 1st column
y = M(:, 2); %Assuming y values are the 2nd column
plot(x, y);
end
hold off;
Are you self taught? Can you recommend any literature/courses?
Thanks again!
OCDER
OCDER 2017-9-7
Glad that you got it to work! I did take a course in college Intro to Matlab, but I'd say 95% self taught. I do what you do - peruse the Q & A section in this website, use the help function A LOT, and try opening and understanding some of matlab built-in codes (which are made by VERY experienced people). Considering you're able to understand and adjust my code to make it work, you're doing great!

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2017-9-6
  2 个评论
S B
S B 2017-9-6
Hi Walter,
Thanks for this; I had just found this too, but have trouble as my files are .asc and everything is tailored to other file formats; am I overlooking something?
Walter Roberson
Walter Roberson 2017-9-6
.asc is not a standardized file format. We would need to know the data format in the files. Is there a header? How many columns? What is the delimiter between the columns?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by