CFD Online Discussion Forums

CFD Online Discussion Forums (
-   CFX (
-   -   Matlab contour plots (

Chris January 12, 2007 09:11

Matlab contour plots
Hi everyone;-)

I have a problem getting matlab to plot the velocity contour which I can see in CFX POST. I tried exporting x,y,z and the velocity vector (.csv) at a giving plane but I can't seem to get the same contour plot in matlab (when using the "contour" command).

Does anyone know how to do this?

Thanks in advance

Regards Chris

Robin January 12, 2007 11:44

Re: Matlab contour plots
Check that you are using the same range, number of contours and a similar color map. Did you plot Hybrid or Conservative values and export the same?


Chris January 12, 2007 12:46

Re: Matlab contour plots
Hi Robin,

Thanks for your answers but my problem is more Matlab related. I exported hybrid values but my problem is when I plot the values in matlab using the "contour" command I get a totally scattered image... Nothing that even resembes the contour found in POST Viewer!

Do you know some commands in matlab that are useful when using it as a visulization tool?

thanks anyways;-)

Robin January 12, 2007 14:25

Re: Matlab contour plots
Sorry, been too long since I used Matlab.

Chris January 12, 2007 15:27

Re: Matlab contour plots
OK no prob.

thanks anyways....:)

Oli January 15, 2007 05:03

Re: Matlab contour plots

I'm a relatively experienced MATLAB user; contour or contourf will work fine, as long as you are inputting the correct data in the arrays.

Can you given an example of your MATLAB code, perhaps with a picture of the results you are getting?


Chris January 15, 2007 09:58

Re: Matlab contour plots
Hi Oli

I get this error:

??? Attempted to access x(:,0); index must be a positive integer or logical.

Error in ==> specgraph.contourgroup.schema>LdoDirtyAction at 256 refresh(h);

??? Error occurred while evaluating listener callback.

when I use this code in Matlab:

clear all; close all; clc;

% Importing data from CFX

filename={'..\CSV\proeve\VelocityPlane1_60lMin379e 3Elements'} extension='.csv'; for i=1:length(filename)





[rows, cols]=size(Data);

%function call for plot format






% [y,index]=sort(y); % x=x(index); % z=z(index); % u=u(index);

D=[y z u];



The csv file comes directly from CFX and the data is imported correctly. The plane I want to make the contour in is the y,z-plane and jeg would like to see the velocity(u) as the contours...

Hope you can help me!

Oli January 16, 2007 10:35

Re: Matlab contour plots

The specific error message you get is because you are trying to reference column 0 in the x array, which does not exist.

However, I think there are a few problems with your code, although I think it would be easier if I just posted some code I got to work. I don't have MATLAB available right now, but I'll have a look over it tonight.

Can I just ask so that I can clarify: are you outputting data from a plane in CFX? If so, doesn't the csv file just have 4 columns (x, y, z and U)? Your code suggests you have 5 columns.

Will post again soon,


chris January 16, 2007 16:34

Re: Matlab contour plots
Hi Oli

The five columns are due to a column containing the node number! It's like an index number (1,2,3,4,5...) - the .csv data is exported as follows:

Node Number(:,1) X(:,2) Y(:,3) Z(:,4) Velocity(:,5)

Hope this helps..

I am not sure that I understand the part you wrote about reference to column 0 in the x array, but I will try looking at it.. Regards Chris

Oli January 17, 2007 03:30

Re: Matlab contour plots

I underestimated the amount of work on at the moment, so I can't run any code myself. However, I can give you a few tips and direct you to some resources you might find useful. Please note that I don't have a copy of MATLAB on me so this is from memory!

1) First of all, the error message:

??? Attempted to access x(:,0); index must be a positive integer or logical.

...occurs because 0 is not a positive integer or logical number. All arrays in MATLAB start at number 1, so x(:,1) would be fine, as long as x is dimensioned that way.

2) The reading of the csv file can be done much less laboriously using the function 'csvread'. Either look in the help, or type 'help csvread' at the command line. Also, the curly brackets around filename are unnecessary: try filename='thisisthefile.csv' then data=csvread(filename) or even data=csvread('thisisthefile.csv'). To add directory information, use square brackets: directory='C:\Work\etc\' then filename=[directory 'thisisthefile.csv'].

3) The reason why contourf doesn't work is that you are only inputting one-dimensional arrays into it. Again, look in the help file or type 'help contour'. Basically, x and y need to define a *structured* (by that I mean 'monotonic') grid, much like the one supplied by the function 'meshgrid' (look it up). Unfortunately, the CFX data is not in this format as it is unstructured, so you need to impose this data upon a structured grid produced by meshgrid using something like the function 'griddata'.

4) Finally, you don't need any 'for' loops for this function. Actually, you very rarely need 'for' loops in MATLAB, and should try to avoid them wherever possible. Your code should just be:

a. START: clear all, close all etc. etc. b. READ THE CSV FILE: use 'csvread' c. DEFINE THE CONTOUR GRID: use 'meshgrid' d. SUPERIMPOSE YOUR DATA ONTO THE CONTOUR GRID: use 'griddata' e. PLOT THE DATA: use 'contourf' with the x and y values from meshgrid, and the variable values from griddata. f. END

No for loops required!!

I recommend these sources for more information:

1) The MATLAB help file is usually very good - I use it often. Search for the page that has a list of functions on and make that one of your favourites.

2) Failing that, try the online help:

3) Failing that, there is a very active message board:

MATLAB is a really great language for visualisation - stick with it, even if it has a rather steep learning curve to being with. You wont regret it!


Chris January 22, 2007 04:43

Re: Matlab contour plots
Hi Oli,

Thanks for your detailed answer " really nice to get something concrete to go on…

I have been working all weekend tying to get a decent contour plot but I still have some problems though…

The problem is that the contour isn't displayed nicely (it is jagged on the boundaries) and takes quit a while for matlab to calculate.

I think the problem is that the matrices produced by meshgrid are not equally spaced. This means that a lot of interpolated points from "meshgrid" are coinciding and resulting in a jagged outer edge.

I tried fixing this by interpolating the interpolated values from "meshgrid" onto an equally spaced matrix using "linspace" and "interp2" unfortunately I get an error message saying I should use "meshgrid" to create 'X' and 'Y' matrices in the "interp2" function " but I did this all ready….

This is the code I am using:

clear all; close all; clc;

% Importing data from CFX

% Hybrid values







[YIH ZIH] = meshgrid(yH,zH); UIH = griddata(yH,zH,uH,YIH,ZIH,'cubic');

yiH=linspace(min(yH),max(yH),100); %equally spaced vectors ziH=linspace(min(zH),max(zH),50);

[YiH ZiH]=meshgrid(yiH,ziH); UiH=interp2(YIH,ZIH,UIH,YiH,ZiH); % This doesn't work!! Any ideas?

figure contourf(YiH,ZiH,UiH,'LineStyle','none') axis equal hold on scatter(YIH(:),ZIH(:),30,UIH(:),'filled','MarkerEd geColor','k') % to see interpolated points hold off

Error message Error using ==> interp2 X and Y must be monotonic vectors or matrices produced by MESHGRID.

Do you have any idea why I can't use the "interp2" function here?

Thanks in advance

Regards Chris

afikr July 31, 2017 13:23

2 Attachment(s)
Hello guys,

Have you guys managed to find a solution to this problem.

I am stuck on the same problem.

What I did:

I extracted data from a sliced plane.

Then I changed it to a radial theta coordinate.

Next, I created a finer structured grid using meshgrid function,

Use griddata function to interpolate.

I have tried all types of interpolation , linear, nearest, cubic and natural. But my contour does not look like it shoud. (See image attached).

any help is much appreciated. Thanks

All times are GMT -4. The time now is 20:32.