Create table using Matlab fprintf - matlab

Suppose I have four vectors x,y,z,c
How do I get matlab to display it using fprintf in a table form with titles above each column like "title 1" and the x column below it.

Here is a short example to get you going. I suggest reading the docs about fprintf also.
clear
clc
%// Dummy data
x = .1:.1:1;
y = 2:2:20;
z = x+y;
%// Concatenate data
A = [x; y ; z];
%// Open file to write
fileID = fopen('MyTable.txt','w');
%// Select format for text and numbers
fprintf(fileID,'%6s %6s %6s\n','x','y','z');
fprintf(fileID,'%.2f \t %.2f \t %.2f\n',A);
fclose(fileID);
Checking what MyTable looks like:
type ('MyTable.txt');
x y z
0.10 2.00 2.10
0.20 4.00 4.20
0.30 6.00 6.30
0.40 8.00 8.40
0.50 10.00 10.50
0.60 12.00 12.60
0.70 14.00 14.70
0.80 16.00 16.80
0.90 18.00 18.90
1.00 20.00 21.00
Hope that helps!

Related

Octave - why is surf not working but trisurf does?

I am able to plot a trisurf chart, but surf does not work.
What am I doing wrong?
pkg load statistics;
figure (1,'name','Matrix Map');
colormap('hot');
t = dlmread('C:\Map3D.csv');
tx =t(:,1);ty=t(:,2);tz=t(:,3);
tri = delaunay(tx,ty);
handle = surf(tx,ty,tz); #This does NOT work
#handle = trisurf(tri,tx,ty,tz); #This does work
`error: surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length
(X)
My data is in a CSV (commas not shown here)
1 2 -0.32
2 2 0.33
3 2 0.39
4 2 0.09
5 2 0.14
1 2.5 -0.19
2 2.5 0.13
3 2.5 0.15
4 2.5 0.24
5 2.5 0.33
1 3 0.06
2 3 0.44
3 3 0.36
4 3 0.45
5 3 0.51
1 3.5 0.72
2 3.5 0.79
3 3.5 0.98
4 3.5 0.47
5 3.5 0.55
1 4 0.61
2 4 0.13
3 4 0.44
4 4 0.47
5 4 0.58
1 4.5 0.85
surf error message is different in Matlab or in Octave.
Error message from Matlab:
Z must be a matrix, not a scalar or vector.
The problem is pretty clear here since you specified Z (for you tz) as a vector.
Error message from Octave:
surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)
You are wrong here since on your example, columns (Z) = 1, but length (X) = 26, so here is the mistake.
One of the consequences of that is that with surf you cannot have "holes" or undefined points on your grid. On your case you have a X-grid from 1 to 5 and a Y-grid from 2 to 4.5 but point of coordinate (2, 4.5) is not defined.
#Luis Mendo, Matlab and Octave do allow the prototype surf(matrix_x, matrix_y, matrix_z) but the third argument matrix_z still have to be a matrix (not a scalar or vector). Apparently, a matrix of only one line or column is not considered as a matrix.
To solve the issue, I suggest something like:
tx = 1:5; % tx is a vector of length 5
ty = 2:0.5:4.5; % ty is a vector of length 6
tz = [-0.32 0.33 0.39 0.09 0.14;
-0.19 0.13 0.15 0.24 0.33;
0.06 0.44 0.36 0.45 0.51;
0.72 0.79 0.98 0.47 0.55;
0.61 0.13 0.44 0.47 0.58;
0.85 0. 0. 0. 0.]; % tz is a matrix of size 6*5
surf(tx,ty,tz);
Note that I had to invent some values at the points where your grid was not defined, I put 0. but you can change it with the value you prefer.

intersection of interpolated line and an interpolated curve matlab

I have astraightline with the following data
xinter=[1.13 1.36 1.62 1.81 2.00 2.30 2.61 2.83 3.05 3.39]
yinter=[0.10 0.25 0.40 0.50 0.60 0.75 0.90 1.00 1.10 1.25]
and I want to find the intersection with a result of an interpolated data
of a curve such as below
a50= [0.77 0.73 0.77 0.85 0.91 0.97 1.05 1.23 1.43 1.53 1.62 1.71 1.89 2.12 2.42];
a25= [0.51 0.60 0.70 0.80 0.85 0.90 0.96 1.09 1.23 1.30 1.36 1.41 1.53 1.67];
vel25=[0.43 0.35 0.30 0.27 0.25 0.24 0.22 0.21 0.22 0.24 0.25 0.27 0.30 0.35];
vel50=[0.68 0.57 0.49 0.43 0.40 0.38 0.36 0.34 0.36 0.38 0.40 0.43 0.49 0.57 0.68 ];
% back up original data, just for final plot
bkp_a50 = a50 ; bkp_vel50 = vel50 ;
% make second x vector monotonic
istart = find( diff(a50)>0 , 1 , 'first') ;
a50(1:istart-1) = [] ;
vel50(1:istart-1) = [] ;
% prepare a 3rd dimension vector (from 25 to 50)
T = [repmat(25,size(a25)) ; repmat(40,size(a50)) ] ;
% merge all observations together
A = [ a25 ; a50] ;
V = [vel25 ; vel50] ;
% find the minimum domain on which data can be interpolated
% (anything outside of that will return NaN)
Astart = max( [min(a25) min(a50)] ) ;
Astop = min( [max(a25) max(a50)] ) ;
% use the function 'griddata'
[TI,AI] = meshgrid( 25:40 , linspace(Astart,Astop,10) ) ;
VI = griddata(T,A,V,TI,AI) ;
% plot all the intermediate curves
%plot(AI,VI)
hold on
% the original curves
%plot(a25,vel25,'--k','linewidth',2)
%plot(bkp_a50,bkp_vel50,'--k','linewidth',2)
% Highlight the curve at T = 30 ;
c30 = find( TI(1,:) == 40 ) ;
plot(AI(:,c30),VI(:,c30),'--r','linewidth',2)
xinter=[1.13 1.36 1.62 1.81 2.00 2.30 2.61 2.83 3.05 3.39]
yinter=[0.10 0.25 0.40 0.50 0.60 0.75 0.90 1.00 1.10 1.25]
x1inter=(AI(:,c30))';
y1inter=(VI(:,c30))';
yy2 = interp1(xinter, yinter, x1inter,'spline')
plot(xinter,yinter, '--k','linewidth',2)
idx = find((y1inter - yy2) < eps, 1); %// Index of coordinate in array
px = x1inter(idx)
py = y1inter(idx)
plot(px, py, 'ro', 'MarkerSize', 18)
But there is an error in the result when I modify x1inter
You can use piecewise polynomial curvefitting and the fzero function to find the intersection point:
pp1 = pchip(xinter,yinter); % Curve 1
pp2 = pchip(AI(:,c30),VI(:,c30)); % Curve 2
fun = #(x) ppval(pp1,x) - ppval(pp2,x); % Curve to evaluate
xzero = fzero(fun,mean(xinter)) % intersection x value
yzero = ppval(pp1,xzero)
plot(xzero, yzero, 'bo', 'MarkerSize', 18)

Smooth data and graph in MATLAB [duplicate]

Using MATLAB, I have a matrix (data) and am plotting using imagesc(data) to produce a heatmap:
data = [1 1 1 1 1 1 1 1 1 1; 1 1.04 1.04 1.04 1.03 1 1.01 1.01 1.03 1.01; 1.36 1.3 1.25 1.2 1.15 1.1 1.2 1.13 1.07 1.11; 3.65 3.16 2.94 2.68 2.39 2.22 2.17 1.95 1.79 1.81; 5.91 5.75 5.47 5.3 4.98 4.79 4.62 4.55 4.38 4.19; 6 6 5.99 5.83 5.49 5.33 5.14 4.94 4.77 4.74];
imagesc(data)
Is there a way to 'smooth' the pixels in order to produce something more like this:
interp2 may be of use here. Use the data as key points, then create a finer grid of points that span the same width and height and interpolate in between the key points.
Something like this:
%// Define your data
data = [1 1 1 1 1 1 1 1 1 1; 1 1.04 1.04 1.04 1.03 1 1.01 1.01 1.03 1.01; 1.36 1.3 1.25 1.2 1.15 1.1 1.2 1.13 1.07 1.11; 3.65 3.16 2.94 2.68 2.39 2.22 2.17 1.95 1.79 1.81; 5.91 5.75 5.47 5.3 4.98 4.79 4.62 4.55 4.38 4.19; 6 6 5.99 5.83 5.49 5.33 5.14 4.94 4.77 4.74];
%// Define integer grid of coordinates for the above data
[X,Y] = meshgrid(1:size(data,2), 1:size(data,1));
%// Define a finer grid of points
[X2,Y2] = meshgrid(1:0.01:size(data,2), 1:0.01:size(data,1));
%// Interpolate the data and show the output
outData = interp2(X, Y, data, X2, Y2, 'linear');
imagesc(outData);
%// Cosmetic changes for the axes
set(gca, 'XTick', linspace(1,size(X2,2),size(X,2)));
set(gca, 'YTick', linspace(1,size(X2,1),size(X,1)));
set(gca, 'XTickLabel', 1:size(X,2));
set(gca, 'YTickLabel', 1:size(X,1));
%// Add colour bar
colorbar;
The code that's at the bottom is required because defining the finer grid ultimately increases the size of the image. I need to relabel the axes to go back to the original size.
We get this:
Small Note
I'm using MATLAB R2014a, and the default colour map is jet. You're using R2014b+ and the default colour map is parula. You won't get the same colour distribution as me, but you will get the smoothness you desire.
You can also use the pcolor(data) function with shading interp to make the color resolution smooth. No need to interpolate the data before plotting.

two matrix output into a single txt file in matlab

I've two matrix output from two Matlab scripts and I'd like to write both results in different columns of the same GUI output in a txt file.
Could you please help me?
I tried some different methods (try to create cell array, or use fprintf for arrays of different sizes) and understood that #GameOfThrows's method is really works.
I realize it in this way:
x = [1 2 3 4 5];
y = [10 20 30 40 50 60 70 80 90];
[m,i] = max( [numel(x) numel(y)]);
if i == 1
y(end+1:numel(x))=NaN;
else
x(end+1:numel(y))=NaN;
end
a = [x; y];
fileID = fopen('data1.txt','w');
fprintf(fileID,'%6.2f %12.2f\r\n',a);
My data1.txt:
1.00 10.00
2.00 20.00
3.00 30.00
4.00 40.00
5.00 50.00
NaN 60.00
NaN 70.00
NaN 80.00
NaN 90.00

matlab: change matrix

I have a code that load cell array and convert them to matrix.
now this matrix shows 4 numbers after floating point for example
0 5 15 1 51,9000 3,4000
0 5 15 1 51,9000 3,4000
0 5 15 1 51,9000 3,4000
how can I change all af the rows to just show 2 numbers after the floating point ?
please consider that I want to change the matrix not print it in command window !
If you want to see it in the command window/editor for debugging purposes, use bank format:
format bank;
Example:
A =[ 51.213123 6.132434]
format bank
disp(A);
Will result in :
A =
51.21 6.13
Also, you can use sprintf
A = [51.900 3.4000];
disp(sprintf('%2.2f ',A));
x = [0 5 15 1 51.9000 3.4000
0 5 15 1 51.9000 3.4000
0 5 15 1 51.9000 3.4000];
fprintf([repmat('%.2f ',1,size(x,2)) '\n'], x')
0.00 5.00 15.00 1.00 51.90 3.40
0.00 5.00 15.00 1.00 51.90 3.40
0.00 5.00 15.00 1.00 51.90 3.40