ths1104 geeké

Write data to a .txt or .csv file with Matlab

Posted in Matlab by ths1104 on April 14, 2014

Windows 7 (x64)
Matlab 2011a

To write data to a .txt or .csv file with Matlab :

% TEMPLATE – Write data from txt file

clear all, close all, clc

%Generate some data
data1 = 0:1:10;
data2 = 10:1:20;
data3 = 20:1:30;

%csv file parameters
csv_filename= [‘xxx.csv’];
csv_header = ‘%head1,head2,head3′;
C = [data1′,data2′,data3′];

%write to csv file
dlmwrite(csv_filename,csv_header,”); %write header to csv_filename
dlmwrite(csv_filename,C,’-append’,’delimiter’,’,’,’precision’,16); %append the data to csv_filename

Advertisements
Tagged with: , ,

Read data from a .txt or .csv file with Matlab

Posted in Matlab by ths1104 on March 24, 2014

Windows 7 (x64)
Matlab 2011a

Use readtxt function or used custom script below (especially if you want to skip headlines).

To read data from a .txt or .csv file with Matlab :

% TEMPLATE – Read and plot data from txt file

%% Read data
%Some parameters for import
DELIMITER = ‘,’;
HEADERLINES = 2;
%load data
input_filename= [‘dataXXX.csv’];
D = importdata(input_filename, DELIMITER, HEADERLINES);
%downsample (if too many points)
%id = round(linspace(1, length(D.data(:,1)), 200))
t = D.data(:,1); %[s]
x = D.data(:,2); %[]

%% Plot data
figure()
hold on, box on, grid on
plot(t,x)

Tagged with: , ,

Run Ansys from Matlab

Posted in Ansys, Matlab by ths1104 on July 25, 2013

Windows 7 (x64)
ANSYS 13.0

To launch an Ansys file (.mac) from Matlab :

    1. Run Ansys “Mechanical APDL Product Launcher”
    2. In the section “Simulation environment”, select “Ansys batch”
    3. In the File Management tab, select you working directory (where the file .mac is located)
    4. In the File Management tab, select you input file (this is you file .mac)
    5. Go to Tools > Display command line
    6. Copy the command line #cmd
    7. Open Matlab and run:

      dos(‘#cmd’)

    8. Matlab should return Ans = 0
    9. If Matlab returns Ans = 100, make sure you can run the file directly in Ansys.
Tagged with: , , ,

Plot multi-column legend – Matlab

Posted in Matlab by ths1104 on April 2, 2013

Matlab 2007a

If you want to plot a multi-column legend, you can use the following trick,

% Some data
x = 1:10;
y1 = x;
y2 = 2*x;
y3 = 3*x;
y4 = 4*x;

% Plot
figure(), hold on;
h(1) = plot(x,y1,’b’);
h(2) = plot(x,y2,’r’);
h(3) = plot(x,y3,’k’);
h(4) = plot(x,y4,’g’);

% Legends plot
% Block 1
ah1 = gca;
legend(ah1,h(1),’y1′,1)
% Block 2
ah2 = axes(‘position’,get(gca,’position’), ‘visible’,’off’);
legend(ah2,h(2),’y2′,2)
% Block 3
ah3 = axes(‘position’,get(gca,’position’), ‘visible’,’off’);
legend(ah3,h(3),’y3′,3)
% Block 4
ah4 = axes(‘position’,get(gca,’position’), ‘visible’,’off’);
legend(ah4,h(4),’y4′,4)

Here is the result,

LegendMultiColumn

 

Tagged with: ,

Change color line of legend – Matlab

Posted in Matlab by ths1104 on November 4, 2011

Matlab 2007a

If you want to change the color of the line of the legend, you can use the following trick,

%Some data
x=0:0.1:10
y1=sin(x);
y2=cos(x);

%Create figure
figure
hold on

plot(x,y1,'--gx')
plot(x,y2,'-bo')

%Add legend
h=legend('legend 1','legend 2');

%Set line of legend in red
leg_line=findobj(h,'type','Line')
for i = 1:length(leg_line)
     set(leg_line(i), 'Color', 'r');
end

Here is the result,

Tagged with: , ,

Automatically plot graphs with different colors in Matlab

Posted in Matlab by ths1104 on November 1, 2011
Tagged with:

Error ‘Undefined function or variable “ExcelWorkbook”.’ with Matlab

Posted in Matlab by ths1104 on August 4, 2011

Generate antidiagonal matrix with Matlab

Posted in Matlab by ths1104 on March 9, 2011

Replace all Infs by zeros in a matrix with Matlab

Posted in Matlab by ths1104 on September 1, 2010

Draw a polyhedron with Matlab

Posted in Matlab by ths1104 on August 25, 2010

Matlab R2007a

To draw a polyhedron,

  1. define the vertices matric
  2. define the faces matrix
  3. draw the polyhedron using the patch function

The following code permits to obtain this polyhedron :

% coordinates of the vertices
v = [
-1 0 0 %vertex 1
1 0 0 %vertex 2
1 1 0 %vertex 3
0 1 0 %…
0 0 1
1 0 1
1 1 1
1 1 1
];

% faces are defined by vertex number
f = [
1 2 6 5 %face 1
2 3 7 6 %face 2
3 4 8 7 %face 3
4 1 5 8 %…
1 2 3 4
5 6 7 8
];

%Draw polyhedron
h = patch(‘vertices’,v,’faces’,f,’facecolor’,’r’)
grid; box; % default view with grid
daspect([1 1 1])
xlabel(‘x’)
ylabel(‘y’)
zlabel(‘z’)
view(3)

%d bloggers like this: