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
Related
My cell array,it contain values like
a=['10100011' '11000111' 00010111' 11100011 '];
I want to apply xor operation ;
I used setxor. I want to xor first value of array i.e 10100011 to all values of cell arrays,required input & output is as follows !
**setxor(10100011, 10100011) =00000000%(first value xor result with first value)
setxor(10100011 , 11000111) =1100100%(first val xor result with second value)
setxor(10100011, 00010111 )=10110100%(first val xor result with third value)
setxor(10100011 ,11100011)=1000000 %(first val xor result with forth value)**
but i don't know how to pass full cell array and single value, i tried to use cellfun but that's not working. I want something that setxor(first val of my cell array , all vals of my cell array) as my cell array is 16x16. Your help would be highly appriciated.
If you're starting from data that looks like this:
A = [163 215 9 131 248 72 246 244 179 33 21 120 153 177 175 249; ...
231 45 77 138 206 76 202 46 82 149 217 30 78 56 68 40];
And you want to XOR the bits of the first entry with every other entry, you don't have to convert A using dec2bin. You can just use bitxor, then format the result however you want (decimal or cell arrays of binary strings):
>> decOut = bitxor(A(1), A)
decOut =
0 116 170 32 91 235 85 87 16 130 182 219 58 18 12 90
68 142 238 41 109 239 105 141 241 54 122 189 237 155 231 139
>> binOut = reshape(cellstr(dec2bin(decOut)), size(A))
binOut =
2×16 cell array
Columns 1 through 10
'00000000' '01110100' '10101010' '00100000' '01011011' '11101011' '01010101' '01010111' '00010000' '10000010'
'01000100' '10001110' '11101110' '00101001' '01101101' '11101111' '01101001' '10001101' '11110001' '00110110'
Columns 11 through 16
'10110110' '11011011' '00111010' '00010010' '00001100' '01011010'
'01111010' '10111101' '11101101' '10011011' '11100111' '10001011'
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]
I would like to partly load a PTX file by matlab (please see the following example)
I need to read and write the first two row (2 numbers) into 2 variables say a and b. And read and write the data from 5th row to the end into a matrix
Thanks for your help
114
221
1 0 0
1 0 0 0
-5.566405 -7.161944 -1.144557 0.197208 24 29 35
-5.560656 -7.154540 -1.137673 0.222400 29 32 39
-5.559846 -7.153491 -1.131895 0.254002 37 40 49
-5.560894 -7.154833 -1.126452 0.305013 51 54 63
-5.560084 -7.153783 -1.120633 0.290013 72 76 88
-5.561128 -7.155119 -1.115189 0.243214 105 113 134
-5.563203 -7.157782 -1.109926 0.227604 130 143 177
-5.569191 -7.165479 -1.105504 0.201602 121 140 173
-7.833616 -10.078705 -1.546952 0.130007 94 112 134
Look at the tdfread function in order to get the data into Matlab. It should be something like datafile = tdfread(filename, '\t'). Once you have that, index into the variable returned from that function like
a = datafile(1, 1);
b = datafile(2, 1);
data = datafile(5:end, :);
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
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