How to import pts file in Matlab - matlab

I have a pts file with 2d points (x,y) that looks like this:
version: 1
n_points: 5
{
159.128 108.541
230.854 109.176
164.841 179.633
193.404 193.597
192.769 229.143
}
How can I read this file and import this data into variables in Matlab?
Thanks.

i would do it like that
FileId=fopen(Filename)
npoints=textscan(FileId,'%s %f',1,'HeaderLines',1)
points=textscan(FileId,'%f %f',npoints{2},'MultipleDelimsAsOne',1,'Headerlines',1)
% now you have the values you want you can put them in a matrix or any variable
Y=cell2mat(C);

Related

Create variable from excel file

I have an excel file with what I would like to become variables in column 1 and values in column 2:
Plot.Shift 20
Plot.MarkerSize 6
Plot.MarkerColor black
Plot.MarkerFaceColor black
Plot.FontSize 22
I read the file with
s=readcell('PlotOptions.xlsx','Sheet','PlotOptions');
I would like to have new variable, Plot in the MATLAB code such as:
>> Plot.Shift % Set it to 20
>> Plot.MarkerSize % Set to 6
Using cell2struct:
s=readcell('PlotOptions.xlsx','Sheet','PlotOptions');
field_names=regexp(s(:,1),'(?<=\.)[A-Za-z]+$','match','once');
Plot = cell2struct(s,field_names);
Plot(1) = [];
Plot is a struct with the desired fields:
>> Plot.Shift
20
>> Plot.MarkerFaceSize
6
...
Consider using cell2struct to convert your heterogeneous data from into a struct array.
From the documentation, sturcts allow you to refer to each item through the requested dot notation while maintaining support for heterogeneous data types.
structArray = cell2struct(cellArray, fields, dim)

Plot means over grouped boxplot in MATLAB?

I am using multiple_boxplot function to generate grouped boxplots:
http://au.mathworks.com/matlabcentral/fileexchange/47233-multiple-boxplot-m
However, instead of medians I want to plot means. First I tried general method:
plot([mean(x)],'dg');
But it did not work. I tried to extract the means and then plot them but that also is not working.
m=[];
for i=1:max(group)
idx=find(group==i);
m=[m nanmean(x(idx))];
end
boxplot(x,group, 'positions', positions);hold on
plot([m],'dg')
What am I doing wrong? And how to plot the means with each boxplot?
Thanks.
You can do the following:
In the function multiple_boxplot change line 48 to:
B = boxplot(x,group, 'positions', positions);
and change the header of the function to:
B = multiple_boxplot(data...
and save the function file.
This won't change anything in how the function works but will let you obtain a handle to the boxplot (B).
Then in your code, create the boxplot as before, but with the output argument B:
B = multiple_boxplot(data...);
And add the following lines:
% compute the mean by group:
M = cellfun(#mean,data);
% convert it to pairs of Y values:
M = mat2cell(repmat(M(:),1,2),ones(size(M,1),1),2);
% change the medians to means:
set(B(6,:),{'YData'},M)

Plotting arrays from a cell list of strings

Suppose I have different rows loaded from a .mat file (using load filename.mat) containing float numbers following the same naming convention, e.g:
file_3try = [ 2.4, 5.2, 7.8 ]
file_4try = [ 8.7, 2.5, 4,2 ]
file_5try = [ 11.2, 9.11 ]
to plot all of these in one plot using automation (I have many more rows than in the example above) I created a cell containing the names of the arrays by using:
name{l} = sprintf('%s%02i%s','file_',num,'try');
inside a for loop with num the numbers in the names and l a counter starting from 1.
But when I try to plot the arrays using for example:
plot(name{1})
I get the error:
Error using plot
Invalid first data argument
Is there a way to solve this, or am I going about this wrong?
There is something built in to solve this
data = load ( 'filename' ); % load data and store in struct
fnames = fieldnames ( data );
for ii=1:length(fnames)
plot ( axHandle, data.(fnames{ii}) );
end
axHandle is a handle to the axes you want to plot on. Its not required but it is good practice to use it. If its not provided then the plot command will plot on the current axes, e.g. gca.
So as mentioned already you need to use eval.
Assuming the file_**X**try rows are different lengths then you could just place all of them in a cell rather than creating a cell of the variable names. So instead of assigning to separate variables the way you are doing you could assign to a cell, so:
file_try{i} = [.....];
You can then cycle through file_try and plot each entry:
for i = 1:length(file_try)
plot(file_try{i});
end
If the rows are not different lengths then stick them in a matrix and plot it.

plotting trajectories using matlab

Im trying to plot trajectories on matlab
My data file (try.txt) that has the trajectories looks like this:
NumofTrajectories
TrajID1 #ofPoints x y x y....
TrajID2 #ofPoints x y x y....
example:
7
0 23 898.6 673.0 859.1 669.9 813.7 667.8 776.8 664.0 739.8 662.1 699.9 654.7 664.5 649.6 625.3 645.5 588.2 640.6 552.3 634.2 516.6 628.2 477.2 624.3 442.1 613.6 406.7 603.4 369.5 599.8 332.7 594.1 297.4 585.2 258.6 583.7 224.1 573.1 191.2 556.8 152.7 554.0 115.1 546.0 79.6 535.8
1 8 481.4 624.9 445.9 596.3 374.5 573.9 354.2 541.0 334.2 508.9 327.6 474.1 324.6 437.5 324.2 390.3
2 24 151.6 570.8 188.3 556.5 225.1 547.7 257.9 529.4 292.9 509.8 326.8 496.8 356.2 476.0 391.2 463.3 423.7 447.9 455.7 431.8 489.2 416.0 524.3 405.3 560.0 395.9 595.8 385.6 632.8 376.1 671.5 372.0 706.9 361.8 742.3 347.3 778.0 334.5 820.5 336.5 856.5 325.0 894.5 309.5 946.1 309.9 990.5 287.0
3 3 594.2 580.4 566.6 544.3 544.9 509.4
4 5 281.8 661.9 266.8 623.4 246.2 576.4 229.7 541.0 220.9 498.4
5 2 563.6 511.3 532.5 479.7
6 5 571.9 617.7 525.6 576.4 481.0 551.9 456.8 524.2 419.7 474.0
I'm trying to plot this on matlab
my code is as follows:
clc;
clear;
%read the input
importfile('try.txt')
%See how many trajectorys there are convert to number
nTraj=str2num(cell2mat(textdata));
%loop over the trajectories
for i = 1:nTraj
disp(data(i,1));
%print the current trajectory number of points
disp(data(i,2));
%get the x-y coordinates of each trajectory
current_traj=data(i,2);
for j=1:current_traj
points=data(i,3:j*2+2);
end
%print the x-y coordinates of each trajectory
%disp(points);
%seperate the x-y coordinates of each trajectory
x=points(1:2:length(points)-1)
y=points(2:2:length(points))
xlabel('latitude');
ylabel('longitude');
plot(x,y,'r');
grid on ;
hold on;
end
And function importfile:
function importfile(fileToRead1)
%IMPORTFILE(FILETOREAD1)
% Imports data from the specified file
% FILETOREAD1: file to read
DELIMITER = ' ';
HEADERLINES = 1;
% Import the file
newData1 = importdata(fileToRead1, DELIMITER, HEADERLINES);
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
The code sometimes works and usually gives me an error say:
Index exceeds matrix dimensions.
Error in ==> plotTrajectory at 23
points=data(i,3:j*2+2);
Can someone explain the error and tell me how to fix it?
The trajectories in try.txt have different lengths. importdata will use the first line to determine the length of your data. If another line is the longest, this line will be split over several lines of your imported matrix. At least this is what debugging shows. I would suggest you use another method to read your file. For me dlmread works:
%read the input
data = dlmread('try.txt', ' ');
% remove header
data = data(2:end,:);
%See how many trajectorys there are convert to number
nTraj=size(data,1);
You can replace your first lines with this code and remove your importfilefunction.

Read a .txt file with both numerical data and words Matlab

i want to read .txt file in matlab with both data and words
the contents of .txt file are
(title "Particle Tracks")
(labels "Time" "Particle Velocity Magnitude")
((xy/key/label "particle-1")
1e-06 45.4551
2e-06 40.3895
2e-06 44.0437
3e-06 34.9606
4e-06 33.1695
4e-06 35.3499
5e-06 29.9504
6e-06 28.0226
6e-06 35.1794
7e-06 41.2255
....
((xy/key/label "particle-2")
1e-06 43.7789
1e-06 45.0513
2e-06 44.1221
3e-06 37.8328
3e-06 43.6451
4e-06 29.1166
5e-06 41.3342
6e-06 28.7241
6e-06 36.3779
7e-06 31.9631
8e-06 29.2826
9e-06 24.7755
9e-06 24.9516
1e-05 22.7528
1e-05 26.6802
1.1e-05 34.4668
the file extends for 100 particles ,1st column is time and 2nd column is velocity
I intend to find the mean velocity of all the particles at various times of column 1,so basically i want to add corresponding column 2 values and divide them by hundred and display against the the column 1 values which is same for all the hundred particles![enter image description here][2]
thanks
The best way to read text data with a complex structure like this is to use the fscanf function in MATLAB. Follow the documentation and you should be able to read the data into an array that you can used to compute the statistics you wish to find.
Another option might be to read the data in line-by-line and use regular expressions with the regexpi function to extract the data you need.
Suppose your input file is input.txt, then use textscan as follows:
fid = fopen('input.txt');
C = textscan(fid, '%n %n', 'commentStyle', '(');
a = C{1};
b = C{2};,
%# do your computations on vectors a and b
%# for example:
ma = mean(a)
mb = mean(b)
You can use the vectors as you wish, e.g. you can process them 100 by 100 elements. That's up to you.