How do I plot a 3D mesh from multiple csv files?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.
채택된 답변
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
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.
Jon
2019년 8월 13일
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!
Glad to hear it is working for you. If this solved your problem, please accept the answer
Jon
2019년 8월 13일
Thanks!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
