How do you plot sequentially numbered files on one plot?
5 views (last 30 days)
Show older comments
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 Comments
OCDER
on 6 Sep 2017
Edited: OCDER
on 6 Sep 2017
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.
Accepted Answer
OCDER
on 6 Sep 2017
Edited: OCDER
on 6 Sep 2017
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 Comments
OCDER
on 7 Sep 2017
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!
More Answers (1)
Walter Roberson
on 6 Sep 2017
2 Comments
Walter Roberson
on 6 Sep 2017
.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?
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!