3D graph from txt file

Hi All, I try to make 3D graph from text file 'output.txt'. This text file has 6 columns separated by commas and numerous rows. I need 3D graph from first 3 columns, say CX, CY, CZ, which have numerous rows. My code is below, but the graph displays only 1 point, which corresponds to the first row. Would anyone now, please, how to include data from all rows, so they are displayed in the graph? Thank you, Helena
fid = fopen('output.txt');
CX = [];
CY = [];
CZ = [];
temp = textscan(fid, '%f, %f, %f');
disp (temp);
CX(:,1) = cell2mat(temp(1));
CY(:,1) = cell2mat(temp(2));
CZ(:,1) = cell2mat(temp(3));
disp (CX);
disp (CY);
disp (CZ);
fclose(fid);
plot3(CY,CZ,CX,'*');
xlabel('Pressure deficit [100 m]');
ylabel('Water quality deficit [mg/L]');
zlabel('Pump energy costs [AUD]');
grid on;
axis square;

 Accepted Answer

Yao Li
Yao Li on 17 Apr 2013
I think the problem may result from the cell variable temp. You can try:
temp = cell2mat(textscan(fid, '%f, %f, %f'));
CX(:,1) = temp(:,1);
.
.
.
Since temp is in cell type, I don't think temp(1) is proper , but temp{1}

7 Comments

Hi Yao, Thank you for your answer. I modified the code as below, but the graph still displays 1 point only. I wonder if the problem is not somewhere in textscan? Best Regards, Helena
fid = fopen('output.txt');
CX = [];
CY = [];
CZ = [];
temp = cell2mat(textscan(fid, '%f, %f, %f'));
disp (temp);
CX(:,1) = temp(:,1);
CY(:,1) = temp(:,2);
CZ(:,1) = temp(:,3);
disp (CX);
disp (CY);
disp (CZ);
fclose(fid);
plot3(CY,CZ,CX,'*');
xlabel('Pressure deficit [100 m]');
ylabel('Water quality deficit [mg/L]');
zlabel('Pump energy costs [AUD]');
grid on;
axis square;
Yao Li
Yao Li on 17 Apr 2013
pls. check the variable temp. Is it what you want? Does it contain all the data that you want?
Does you text file contain any headlines or characters? Pls. make sure the text file only contains numeric data.
Helena
Helena on 17 Apr 2013
The textfile containts numerical data only without any headings. The problem is that temp file contains 3 columns (which is OK), yet only 1 row (it needs to have numerous rows). That is why I think the problem is within the textscan.
Yao Li
Yao Li on 17 Apr 2013
I think textscan is OK to load all the data. Is it possible to send the text file to me?
Helena
Helena on 17 Apr 2013
Text file is below (this one has 10 rows and 6 colums):
17303.677782, 0.000000, 87714.752274, 0.000000, 6.000000, 17.000000
16842.270882, 224.131285, 83427.626617, 0.000000, 8.000000, 14.000000
16842.270882, 224.131285, 83427.626617, 0.000000, 8.000000, 14.000000
16842.270882, 224.131285, 83427.626617, 0.000000, 8.000000, 14.000000
14255.421024, 0.000000, 89672.334839, 0.000000, 8.000000, 18.000000
15677.842565, 0.000000, 85528.593079, 0.000000, 10.000000, 18.000000
16842.270882, 224.131285, 83427.626617, 0.000000, 8.000000, 14.000000
16842.270882, 224.131285, 83427.626617, 0.000000, 8.000000, 14.000000
14255.421024, 0.000000, 89672.334839, 0.000000, 8.000000, 18.000000
14255.421024, 0.000000, 89672.334839, 0.000000, 8.000000, 18.000000
Yao Li
Yao Li on 17 Apr 2013
Edited: Yao Li on 17 Apr 2013
You may have to define the delimiter as follows:
temp = cell2mat(textscan(fid, '%f%f%f','delimiter', ','));
Thank you, just a small modification:
temp = cell2mat(textscan(fid, '%f%f%f%f%f%f','delimiter', ','));
otherwise it loads columns 4-6 from the original txt file into the temp as extra rows, so I get 3 columns (which is OK), but 20 rows of data instead of 10 rows. So the final code is:
fid = fopen('output.txt');
CX = [];
CY = [];
CZ = [];
temp = cell2mat(textscan(fid, '%f%f%f%f%f%f','delimiter', ','));
disp (temp);
CX(:,1) = temp(:,1);
CY(:,1) = temp(:,2);
CZ(:,1) = temp(:,3);
disp (CX);
disp (CY);
disp (CZ);
fclose(fid);
plot3(CY,CZ,CX,'*');
xlabel('Pressure deficit [100 m]');
ylabel('Water quality deficit [mg/L]');
zlabel('Pump energy costs [AUD]');
grid on;
axis square;
Thanks a lot for you help!

Sign in to comment.

More Answers (1)

son nguyen xuan
son nguyen xuan on 21 Aug 2019

0 votes

how can i get range of color for plot3?
thanks

Categories

Find more on Block and Blockset Authoring in Help Center and File Exchange

Asked:

on 17 Apr 2013

Answered:

on 21 Aug 2019

Community Treasure Hunt

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

Start Hunting!