When matlab coordinate is disagreed with file coordinate generated from external software

I use Matlab to visualize the data in 3D. The data is generated by external software. I think I followed well the syntex upon slice function for 3D graphics. However, when slice function applies, the results is not right to the read file. I mean that Matlab coordinate shows x, y, z in clockwise. But when Matlab read the file, the 3D graphic data showed y,x,z in clockwise. Have you ever had similiar experience with me? Or Am I doing something wrong? I check my code many times but I can not see anything wrong. If you know a solution for this case, would you let me know please?

Answers (1)

How are you doing the reading?
Remember that Matlab arrays are stored down columns not across rows, so if you fill sequential memory locations with values that you read from rows, you are going to be writing down columns.
For example, for a 3 x 2 array, A(1,1) is in memory next to A(2,1) which is next to A(3,1) which is next to A(1,2) which is next to A(2,2) which is next to A(3,2).
1 4 2 5 3 6
which would correspond to reading in the file
1 2 3 4 5 6

4 Comments

Thank you, W.
Regarding how I read the file, for example, the file is ASCII format. I read the file in following way, simply
%%%
for z = 1:length_z
for y = 1:length_y
for x = 1:length_x
data(x,y,z)=file(x,y,z);
end
end
end
Then I put slice function as
CrossSection=slice(data,x,y,z,[Px],[Py],[Pz]);
view(3);
%%%
[Px],[Py],[Pz] are sliced sections of interest in coordinate.
In the original file, I shifted datapoint at some value in Y-axis, but matlab could show data at X-axis shift as length as Y-shift. I was expecting Y shifted data, but it was shifted in X.
Am I still doing something wrong?
Your loop is the same
data = file(1:length_x,1:length_y,1:length_z);
which is probably the same as
data = file;
I think you may have missed showing a step in the reading, as you do not show any calls that do file I/O. Are you using fscanf() or textscan() or textread() or importdata() or load() or xlsread() or dlmread() or ... ?
Here is file-read-method.
%%%
filename = sprintf('originalfile.kkk');
fid = fopen(filename,'r');
temp_data=fgets(fid);
data = sscanf(temp_data, '%f');
%%%
in this way, I read the file. Since my file is ASCII file, I used fgets to read file because fscanf took long and textread did work. I assumed you would guess this reading part. Is it something bad to read the file? How do you think?
That form of reading is going to result in a vector of values, not a 3 dimension array such as would be needed to extract data in the loop you showed.
If you used reshape() on the data to get a 3 dimensional array, then you are going to have exactly the difficulty I noted above: that consecutive values Matlab reads in will be stored down columns.
To fix this issue, use
data = permute(file, [2 1 3]);

This question is closed.

Asked:

on 8 Mar 2011

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!