How do I plot a 3D mesh from multiple csv files?

How do I plot a 3D mesh from multiple csv files?
Here is the code I've been using, found from the internet:
nFiles=101
filenamePrefix='F1--Trace-no loop-0000--0000';
filenameExtension='.csv';
for i = 1:nFiles
filename = [filenamePrefix, int2str(i), filenameExtension];
tmpData=load(filename,'-ascii');
x{i}=tmpData(:,1);
y{i}=tmpData(:,2);
end
mesh(x{i},y{i},i}
This is the error I recieved:
Error using load
Number of columns on line 2 of ASCII file F1--Trace-no
loop-0000--00001.csv must be the same as previous lines.

 Accepted Answer

Jon
Jon on 13 Aug 2019
Edited: Jon on 13 Aug 2019
The problem apparently is with loading the files, nothing to do with plotting 3d mesh, or at least it didn't get far enough to have any problem with that. It is difficulat to tell exactly what is causing the problem without seeing the files you are trying to load.
I think that it is likely that the first line is a header and has a different number of columns
You could try
tmpData = readmatrix(filename, 'NumHeaderLines',1)

11 Comments

this ran, but then came up with this error:
Unable to perform assignment because brace indexing is not
supported for variables of this type.
On what line of your code does this occur. Please cut and paste the entire error message.
Also, the line in your code above
mesh(x{i},y{i},i}
will definitely throw an error as curly bracket on the right does not match up with anything.
I don't know why you are assigning the values using curly braces. This makes cell arrays. You don't need those as inputs to mesh.
In general, I do not understand your code.
If you are going to plot a wireframe mesh for the data in each .csv file then the call to mesh should be inside of your loop.
Also is the "z" value of your mesh really given by the file count. That doesn't make sense to me.
I suggest cleaning up the structure of your code so that it does what you want, and then see what remaining syntax and other errors you have.
the error message is not with any line:
Unable to perform assignment because brace indexing is not supported for variables of this type.
To be honest I am completely inexperienced with MatLAB, just trying to interperate some data for my masters thesis,
the z value will be frequency of kHz in steps of 1kHz, so the file count works.
Please attach one of your data files. Maybe if I look at this I can better understand what you are doing
i've attached the first 2 data files i'm using,
I've got 101 of these files within a folder, and i'm just trying to map them together on a 3D map,
I'm open to anything right now, including a completely different code setup if needed
I think something like this should be close to what you want.
Note, you have to be careful generating the number part of the filename so that it includes leading zeros. I have assumed that you have the same number of data points (421) in all of the files and hard coded that, but you could be more general if needed.
nFiles=101
filenamePrefix='F1--Trace-no loop-0000--';
filenameExtension='.csv';
% preallocate arrays to hold data
A = zeros(421,nFiles);
Hz = zeros(421,nFiles)
T = zeros(421,nFiles)
% loop through the data files assigning data
for i = 1:nFiles
filestr = num2str(i-1,'%05.f')% % include leading zeros e.g. '00001', '00099'
filename = [filenamePrefix, filestr, filenameExtension];
tmpData = readmatrix(filename, 'NumHeaderLines',1)
T(:,i) =tmpData(:,1); % time
Hz(:,i) = i*1000; % assume 1KHz steps
A(:,i) = tmpData(:,2); % amplitude
end
% plot the results
mesh(Hz,T,A)
xlabel('Hz')
ylabel('time')
zlabel('amplitude')
This is a great improvement on what I had previously, this is the new error message coming up
;
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
I would guess this means that some of your files have more samples (time,amplitude) pairs than others. Try making these changes
numPoints = 450; % or what ever you think the maximum will be
% preallocate arrays to hold data
% data files may have different number of points
% allocate for the maximum and
% fill with NaN so unused points will not be plotted
A = NaN(numPoints,nFiles);
Hz = NaN(numPoints,nFiles)
T = NaN(numPoints,nFiles)
you were right, a couple of minor adjustments allowed the code to work, thanks for the help! here was the final code I used:
for i = 10:bnFiles
filename = [filenamePrefix,sprintf('%05d',i-1), filenameExtension];
tmpData = readmatrix(filename, 'NumHeaderLines',1);
x(:,i)=tmpData(:,1);
z(:,i)=tmpData(:,2);
y(:,i) = i*1000;
disp(i)
end
mesh(x,y,z)
xlabel('Hz')
ylabel('time')
zlabel('amplitude')
Thanks again!
Jon
Jon on 13 Aug 2019
Edited: Jon on 13 Aug 2019
Glad to hear it is working for you. If this solved your problem, please accept the answer

Sign in to comment.

More Answers (0)

Asked:

on 13 Aug 2019

Commented:

Jon
on 13 Aug 2019

Community Treasure Hunt

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

Start Hunting!