Load with octave created ASCII format .mat file in Matlab - matlab

As I am running out of licenses I am using both Matlab and Octave and as long as I keep things simple I haven't had any trouble.
I recently started using .mat files to decrease my amount of single files.
When I do everything in Matlab it works just fine but when I use 'save' in Octave it saves the file as ASCII and it looks somewhat like this at the beginning and has multiple matrixes in it and so multiple headers.
# Created by Octave 3.6.4, Mon Feb 24 21:34:39 2014 CET <***#****>
# name: C
# type: matrix
# rows: 10000
# columns: 10
79 79 79 79 79 79 79 79 79 79
74 115 87 55 101 46 83 92 113 61
69 142 128 48 160 45 87 113 114 71
84 107 145 62 245 78 69 88 149 78
120 73 148 32 299 114 57 79 137 76
This is fine but Matlab refuses to read the file. Neither with 'load' and '-ASCII' nor with importdata.
(Warning: File contains uninterpretable
data....)
Is there anything I can do? Octave loads the files just fine with 'load'.
Thanks!!

In Octave save as "-ascii" or as matlab binary format v4, v6, v7
octave:1> a = rand(3, 3)
a =
0.086093 0.541999 0.889222
0.029643 0.633532 0.762954
0.544787 0.150573 0.927285
octave:2> save ("-ascii", "yourfile.asc")
octave:3> save ("-v7", "yourfile.mat")
back in matlab do
>> b = load ('yourfile.asc')
b =
0.0861 0.5420 0.8892
0.0296 0.6335 0.7630
0.5448 0.1506 0.9273
or
>> load ('yourfile.mat')
>> a
a =
0.0861 0.5420 0.8892
0.0296 0.6335 0.7630
0.5448 0.1506 0.9273

Related

How could I have each result as a vector?

I think that I have always the same problem, vector, array and cell array, Thanks to this discussion, How can I split a Text in blocks of 16 bytes every one? I could resolve my first problem. However I still need that each result in data must be e vector in order to encrypt it.
fid = fopen('file.txt', 'r');
alldata = textscan(fid, '%s');
tmp = reshape(alldata{1}, 16, []).';
tmp = arrayfun(#(x)strjoin(tmp(x,:)), 1:size(tmp, 1), 'uniformoutput', false)
key=hex2dec(key_hex).'
data= (cat(1, tmp{:}))
for i= 1:rows(data)
Matrix(i, :)= hex_keys([data(i,1:15), data(i,16)])
chiffrement (Matrix(i,:), key,1)
endfor
endfunction
My error is: error: Plaintext has to be a vector (not a cell array) with 16 elements.
I would be very grateful if you could help me.
The file.txt contains for example:
60 3d eb 10 15 ca 71 be 2b 73 ae f0 85 7d 77 81 60 3d eb 10 15 ca 71 be 2b 73 ae f0 85 7d 77 81 60 3d eb 10 15 ca 71 be 2b 73 ae f0 85 7d 77 81
In fact, it is a big programm and succesion of function, All what i need is how to convert each line in this data result to a vector.
data =
60 3d eb 10 15 ca 71 be 2b 73 ae f0 85 7d 77 81
60 3d eb 10 15 ca 71 be 2b 73 ae f0 85 7d 77 81
60 3d eb 10 15 ca 71 be 2b 73 ae f0 85 7d 77 81
I think you are over complicating the problem, my assumptions is you want to convert your data in file.txt into numbers (i.e. you used hex2dec), so let's do just that and leave Arrayfun out of the problem:
fid = fopen('file.txt', 'r');
alldata = textscan(fid, '%s');
tmp = reshape(alldata{1}, 16, []).'; % here we still parse 16 hex for every row using your function call
tmp = cellfun(#hex2dec,tmp,'un',0) % now we use cellfun to convert all your hex to numbers
Matrix = cell2mat(tmp)
Matrix =
96 61 235 16 21 202 113 190 43 115 174 240 133 125 119 129
96 61 235 16 21 202 113 190 43 115 174 240 133 125 119 129
96 61 235 16 21 202 113 190 43 115 174 240 133 125 119 129
whos Matrix
Name Size Bytes Class Attributes
Matrix 3x16 384 double
Now you can use your for loop to do whatever you want, it becomes regular indexing.

Importing CSV into Matlab

Chemical composition of a certain material
Hi,
I am trying to import the below mentioned data in CSV format in matlab, which is [1000x10] in dimensions.
HCL;H2SO4;CH4; SULPHUR;CHLORINE;S2O3;SO2;NH3;CO2;O2
144 2 3 141 140 6 7 137 136 10 11 133
13 131 130 16 17 127 126 20 21 123 122 24
25 119 118 28 29 115 114 32 33 111 110 36
108 38 39 105 104 42 43 101 100 46 47 97
96 50 51 93 92 54 55 89 88 58 59 85
61 83 82 64 65 79 78 68 69 75 74 72
73 71 70 76 77 67 66 80 81 63 62 84
60 86 87 57 56 90 91 53 52 94 95 49
48 98 99 45 44 102 103 41 40 106 107 37
109 35 34 112 113 31 30 116 117 27 26 120
121 23 22 124 125 19 18 128 129 15 14 132
12 134 135 9 8 138 139 5 4 142 143 1
I am able to import this data through my code
fid = fopen(uigetfile('.csv'),'rt');
FileName = fopen(fid);
headers = fgets(fid); %get first line
headers = textscan(headers,'%s','delimiter',';'); %read first line
format = repmat('%f',1,size(headers{1,1},1)); %count columns n makeformat string
data = textscan(fid,format,'delimiter',';'); %read rest of the file
data = [data{:}];
I am getting data in matrix form in variable data [1000x10] and name of all the components like HCL, H2SO4 in a cell array named headers{1x1}.
Now I have two questions like the built in import feature in matlab you have flexibility to import data as separate column vectors, numeric matrix,cell array and table format. Is it possible to do as such through code, like i get column vectors with their name HCL with [1000x1] and H2sO4 with [1000x1] in my workspace after import and so on all the column vectors with their names with [1000x1]dimensions.
if yes then help me please...?
If above mentioned is not possible then i can do alternatively that now I have names of column vectors in headers cell array, how I can extract those name and use those names as column vector names through code and I can assign data from data matrix [1000x10] to each column vector with their corresponding names.
like if i say
x = headers {1*1}{1*1}; i will get x = "HCL"
x = genvarname(x); I will get x= x0x22HCL0x2 BUT
I want that x get replaced with HCL.and then I assign
HCL = data(:,1) and same like this other variables H2SO4,SULPHUR, CHLORINE.
You can say i try to implement the import feature of column vector through my code.
Kindly help me to solve this issue. thanks
Have you tried the built-in readtable function?
You can access each column of the table by using the named column header.
If you'd like, you can use the two data types to create a table in MatLab. I'm not terribly familiar with its use, but it seems to be well documented. I'm sure someone else can expand upon this.
Edit:
After re-reading your question, I think this is closer to what you are after.
n=10;
what='HCL';%change this to any of the strings you interested in
numstr = repmat('%f',1,n);
hdrstr = repmat('%s',1,n);
headers = textscan(headers,hdrstr,'delimiter',';');
headers = headers(1,:)
data = cell2mat(textscan(fid,numstr,'delimiter',';'));
datout = data(:,strcmp(headers,what));%datout will be 1000x1 HCL data
Depending on what you want to do, you can loop through these appropriately
I know this is not what you asked for, but I would convert to a struct:
x=cell2struct(num2cell(data),headers,2)
reason is simple, selecting for example the third row with individual variables is not possible. With a struct simply use x(3)
If at some point you need the vectors you originally asked for and you can't use the strcut, use [x.HCL]

Function readmtx on matlab

I want to read a matrix that is on my matlab path. I was using the function readmtx but I don't know what to put on 'precision' (mtx = readmtx(fname,nrows,ncols,precision)).
I was wondering if you could help me with that. Or suggest a better way to read the matrix
You could read a matrix from text file with load command. If the first line include text, that should be started with %.
Note that each row of the text file should be values of a row in matrix, which are separated by a space, for Example:
%C1 C2 C3
1 2 3
4 5 6
7 8 9
Then, if you use load command you can read the text file into a matrix, something like:
myMatrix = load('textFileName.txt')
Now, Let's talk about readmtx ;)
About precision as described here:
Both binary and formatted data files can be read. If the file is binary, the precision argument is a format string recognized by fread. Repetition modifiers such as '40*char' are not supported. If the file is formatted, precision is a fscanf and sscanf-style format string of the form '%nX', where n is the number of characters within which the formatted data is found, and X is the conversion character such as 'g' or 'd'. Fortran-style double-precision output such as '0.0D00' can be read using a precision string such as '%nD', where n is the number of characters per element. This is an extension to the C-style format strings accepted by sscanf. Users unfamiliar with C should note that '%d' is preferred over '%i' for formatted integers. MATLAB syntax follows C in interpreting '%i' integers with leading zeros as octal. Formatted files with line endings need to provide the number of trailing bytes per row, which can be 1 for platforms with carriage returns or linefeed (Macintosh, UNIX®), or 2 for platforms with carriage returns and linefeeds (DOS).
Check this example also:
Write and read a binary matrix file:
fid = fopen('binmat','w');
fwrite(fid,1:100,'int16');
fclose(fid);
mtx = readmtx('binmat',10,10,'int16')
mtx =
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
mtx = readmtx('binmat',10,10,'int16',[2 5],3:2:9)
mtx =
13 15 17 19
23 25 27 29
33 35 37 39
43 45 47 49

Matlab .txt file read using space delimiter

I want to read a text file into a matrix using space delimiter.My text file contains information like this:
AJ_Lamas/AJ_Lamas_0001.jpg 58 68 134 134 -2 10 31 43 53 45
Aaron_Eckhart/Aaron_Eckhart_0001.jpg 63 72 126 126 0 10 34 35 53
Aaron_Guiel/Aaron_Guiel_0001.jpg 54 67 144 144 -1 10 34 44 58
Aaron_Patterson/Aaron_Patterson_0001.jpg 47 62 148 148 1 10 44 65 63
Aaron_Peirsol/Aaron_Peirsol_0001.jpg 64 72 127 127 0 10 33 43
I tried :
m=dlmread('D:\MatlabCode\lfw_ffd_ann.txt', ' ')
but it shows some errors:
Error using dlmread (line 139)
Mismatch between file and format string.
Trouble reading number from file (row 1u, field 1u) ==> image_name
face_bbox_x face_bbox_y face_bbox_width face_bbox_height headpose
num_facial_features left_eye_left_x left_eye_left_y left_eye_right_x
left_eye_right_y mouth_left_x mouth_left_y mouth_right
You can't really read it itno a matrix, but into a cell and can achieve it with textscan(). Supposing you want to read the actuall strings (which I assume because of the file names), it would go something like this:
fid=fopen('D:\MatlabCode\lfw_ffd_ann.txt');
C=textscan(fid,'%s','delimiter',' ');
fclose(fid);
hope that helps

How can I retrieve the coordinate from excel and plot the points according to the map? - MATLAB

I'm currently working on map routing system for pedestrian pathway and not sure does anyone did this before. But I have collected various way-points coordinates of the path using google earth.
Below data are the coordinates points for the pathway which is save it on Excel and import to Matlab
1 1.37723400000000 103.839645000000
2 1.37722000000000 103.839741000000
3 1.37723300000000 103.839843000000
4 1.37723600000000 103.839968000000
5 1.37724100000000 103.840125000000
6 1.37723900000000 103.840245000000
7 1.37724900000000 103.840435000000
8 1.37725700000000 103.840674000000
9 1.37724800000000 103.840896000000
10 1.37726500000000 103.841071000000
11 1.37728300000000 103.841190000000
12 1.37729500000000 103.841393000000
13 1.37734600000000 103.841591000000
14 1.37736500000000 103.841918000000
15 1.37739800000000 103.842093000000
16 1.37742900000000 103.842400000000
17 1.37744900000000 103.842629000000
18 1.37748100000000 103.842895000000
19 1.37750200000000 103.843164000000
20 1.37752800000000 103.843428000000
21 1.37756500000000 103.843689000000
22 1.37757000000000 103.843997000000
23 1.37758500000000 103.844248000000
24 1.37749200000000 103.844499000000
25 1.37765800000000 103.844515000000
26 1.37806100000000 103.844467000000
27 1.37853700000000 103.844386000000
28 1.37888000000000 103.844349000000
29 1.37924600000000 103.844303000000
30 1.37957500000000 103.844266000000
31 1.37966200000000 103.844190000000
32 1.37965300000000 103.844034000000
33 1.37963800000000 103.843813000000
34 1.37961600000000 103.843550000000
35 1.37959500000000 103.843313000000
36 1.37957600000000 103.843097000000
37 1.37957300000000 103.843007000000
38 1.37956800000000 103.842852000000
39 1.37953900000000 103.842602000000
40 1.37949900000000 103.842315000000
41 1.37948700000000 103.842056000000
42 1.37953800000000 103.841882000000
43 1.37967800000000 103.841755000000
44 1.37984000000000 103.841674000000
45 1.38007300000000 103.841546000000
46 1.38027900000000 103.841430000000
47 1.38044400000000 103.841354000000
48 1.38064800000000 103.841248000000
49 1.38092400000000 103.841100000000
50 1.38125600000000 103.840942000000
51 1.38178300000000 103.840682000000
52 1.38202300000000 103.840565000000
53 1.38236700000000 103.840396000000
54 1.38271600000000 103.840206000000
55 1.38302800000000 103.840017000000
56 1.38314600000000 103.840222000000
57 1.38299000000000 103.840230000000
58 1.38286700000000 103.840288000000
59 1.38265300000000 103.840398000000
60 1.38242100000000 103.840522000000
61 1.38219900000000 103.840638000000
62 1.38195100000000 103.840766000000
63 1.38162100000000 103.840934000000
64 1.38130200000000 103.841093000000
65 1.38106700000000 103.841202000000
66 1.38088500000000 103.841286000000
67 1.38069300000000 103.841372000000
68 1.38049000000000 103.841494000000
69 1.38018700000000 103.841659000000
70 1.37988900000000 103.841822000000
71 1.37966700000000 103.841963000000
72 1.37965000000000 103.842037000000
73 1.37965100000000 103.842220000000
74 1.37966300000000 103.842363000000
75 1.37968900000000 103.842641000000
76 1.37973000000000 103.843049000000
77 1.37975300000000 103.843305000000
78 1.37978200000000 103.843690000000
79 1.37982300000000 103.844135000000
80 1.37989800000000 103.844222000000
81 1.37996400000000 103.844230000000
82 1.38029900000000 103.844183000000
83 1.38080600000000 103.844091000000
84 1.38119600000000 103.843985000000
85 1.38170500000000 103.843838000000
86 1.38194900000000 103.843765000000
87 1.38220500000000 103.843683000000
88 1.38250000000000 103.843564000000
89 1.38296800000000 103.843388000000
90 1.38367400000000 103.843107000000
91 1.38379700000000 103.842994000000
92 1.38384900000000 103.842791000000
93 1.38376700000000 103.842173000000
94 1.38372500000000 103.841758000000
95 1.38358400000000 103.841212000000
96 1.38350400000000 103.840867000000
97 1.38324600000000 103.840388000000
98 1.38288200000000 103.839785000000
99 1.38265700000000 103.839436000000
100 1.38238700000000 103.838973000000
101 1.38224900000000 103.838785000000
102 1.38215800000000 103.838830000000
103 1.38197900000000 103.838933000000
104 1.38162900000000 103.839097000000
105 1.38115400000000 103.839338000000
106 1.38080900000000 103.839609000000
107 1.38036600000000 103.839756000000
108 1.37995000000000 103.839782000000
109 1.37957400000000 103.839826000000
110 1.37904400000000 103.839883000000
111 1.37844300000000 103.839807000000
112 1.37803700000000 103.839743000000
113 1.37765800000000 103.839690000000
114 1.37735600000000 103.839665000000
115 1.38310100000000 103.840134000000
116 1.38108800000000 103.841146000000
117 1.37961700000000 103.842951000000
118 1.37978200000000 103.844233000000
My Code
M = xlsread('YCKMap.xlsx');
figure(),plot(M(:,2), M(:,3));
RESULT
http://i.stack.imgur.com/0dncY.jpg
What I want:
Is it possible to join these points together
(55 115 56) (49 116 65) (37 117 76) (30 118 80)
As shown in the image above?
I have figure out the answer for my question and would like to benefit the community for what I have come out with. According #Dan code plot(M(:,2), M(:,3); can plot the points out but the orientation of the graph is way off. Therefore I use waypoints function to make it better.
Anyway, organize your excel data by leaving a cell gap when you want to insert a new line.
I have paste the excel data for you to get a clearer picture.
Coding
M = xlsread('excel.xlsx');
waypoints =[M(:,2), M(:,3)];
[lttrk,lntrk] = track('rh',waypoints,'degrees');
figure(),geoshow(lttrk,lntrk,'DisplayType','line', 'color','r');
Excel Data
1 1.377234 103.839645
2 1.37722 103.839741
3 1.377233 103.839843
4 1.377236 103.839968
5 1.377241 103.840125
6 1.377239 103.840245
7 1.377249 103.840435
8 1.377257 103.840674
9 1.377248 103.840896
10 1.377265 103.841071
11 1.377283 103.84119
12 1.377295 103.841393
13 1.377346 103.841591
14 1.377365 103.841918
15 1.377398 103.842093
16 1.377429 103.8424
17 1.377449 103.842629
18 1.377481 103.842895
19 1.377502 103.843164
20 1.377528 103.843428
21 1.377565 103.843689
22 1.37757 103.843997
23 1.377585 103.844248
24 1.377492 103.844499
25 1.377658 103.844515
26 1.378061 103.844467
27 1.378537 103.844386
28 1.37888 103.844349
29 1.379246 103.844303
30 1.379575 103.844266
31 1.379662 103.84419
32 1.379653 103.844034
33 1.379638 103.843813
34 1.379616 103.84355
35 1.379595 103.843313
36 1.379576 103.843097
37 1.379573 103.843007
38 1.379568 103.842852
39 1.379539 103.842602
40 1.379499 103.842315
41 1.379487 103.842056
42 1.379538 103.841882
43 1.379678 103.841755
44 1.37984 103.841674
45 1.380073 103.841546
46 1.380279 103.84143
47 1.380444 103.841354
48 1.380648 103.841248
49 1.380924 103.8411
50 1.381256 103.840942
51 1.381783 103.840682
52 1.382023 103.840565
53 1.382367 103.840396
54 1.382716 103.840206
55 1.383028 103.840017
56 1.383146 103.840222
57 1.38299 103.84023
58 1.382867 103.840288
59 1.382653 103.840398
60 1.382421 103.840522
61 1.382199 103.840638
62 1.381951 103.840766
63 1.381621 103.840934
64 1.381302 103.841093
65 1.381067 103.841202
66 1.380885 103.841286
67 1.380693 103.841372
68 1.38049 103.841494
69 1.380187 103.841659
70 1.379889 103.841822
71 1.379667 103.841963
72 1.37965 103.842037
73 1.379651 103.84222
74 1.379663 103.842363
75 1.379689 103.842641
76 1.37973 103.843049
77 1.379753 103.843305
78 1.379782 103.84369
79 1.379823 103.844135
80 1.379898 103.844222
81 1.379964 103.84423
82 1.380299 103.844183
83 1.380806 103.844091
84 1.381196 103.843985
85 1.381705 103.843838
86 1.381949 103.843765
87 1.382205 103.843683
88 1.3825 103.843564
89 1.382968 103.843388
90 1.383674 103.843107
91 1.383797 103.842994
92 1.383849 103.842791
93 1.383767 103.842173
94 1.383725 103.841758
95 1.383584 103.841212
96 1.383504 103.840867
97 1.383246 103.840388
98 1.382882 103.839785
99 1.382657 103.839436
100 1.382387 103.838973
101 1.382249 103.838785
102 1.382158 103.83883
103 1.381979 103.838933
104 1.381629 103.839097
105 1.381154 103.839338
106 1.380809 103.839609
107 1.380366 103.839756
108 1.37995 103.839782
109 1.379574 103.839826
110 1.379044 103.839883
111 1.378443 103.839807
112 1.378037 103.839743
113 1.377658 103.83969
114 1.377356 103.839665
1 1.377234 103.839645
49 1.380924 103.8411
116 1.381088 103.841146
65 1.381067 103.841202
37 1.379573 103.843007
117 1.379617 103.842951
76 1.37973 103.843049
30 1.379575 103.844266
118 1.379782 103.844233
80 1.379898 103.844222
If you want to plot points in Matlab in a different order from that which they appear in your matrix, append a column vector at the beginning of the matrix that is a vector of the order the point should appear in. Then use the sortrows function of matlab to rearrange the matrix. It will now be in the correct order for plotting. For example:
M = [0 4 2; 1 17 5]';
plot(M);
Now suppose you knew that the correct plot order was (0,1) then (2,5) then (4,17). Compared to the input matrix, M, the correct plot order can be specified by order = [1 3 2]'
So to plot in the correct order:
M = [order'; M'];
M = sortrows(M);
plot(M(:,2), M(:,3);
Which means as long as you know the correct plot order, you can now use this method to get the correct plot.
So in your example you want those points to be plotted in this order (55 115 56) (49 116 65) (37 117 76) (30 118 80). Lets assume you want them to start at 55 as this is the first point in your new order, currently M looks like this: M = [...;30 118 80;...;37 117 76;...;49 116 65;...;55 115 56;...] but you want it to look like this: M = [...; 55 115 56; 56 116 65; 57 117 76; 58 118 80;...]. The problem is that you can't just change the values like this M(M(:,1) == 30,1) = 58 because then you will land up with 2 points indexed at 58. So first you need to add 1 too all the indices that are 58 or higher, so M(M(:,1) >= 58) = M(M(:,1) >= 58) + 1 and then finally M(M(:,1) == 30,1) = 58. So to generalise the changing of a point:
M(M(:,1) >= newIndex) = M(M(:,1) >= newIndex) + 1;
M(M(:,1) == oldIndex,1) = newIndex;
If there is some logic that define how you change the points, then you can write this into a loop. If not and it is just those 4 points, then just run this manually in the command line.