Read from .mat file - matlab

working with MATLAB, I have a Matrix M, I want to save it in a .mat file then import it and reuse it in my code. I have tried:
save ('file1.mat','M')
MSaved=load('file1.mat');
Name | Size | Bytes | Class | Attributes
-------+---------+-------+--------+-----------
MSaved | 4215x20 | 40744 | double |
How can I get access to each cell of the matrix? How to save it again in a matrix?
Need some help, thanks in advance.

The load function will return a structure containing all the saved variables. So your matrix is MSaved.M

Related

How to use different marker colors in a single probplot using Matlab?

I am using the probplot command in Matlab to obtain the probability plot of a a single dataset . There are 15 data points ( all numbers), I would like to use different markercolors for different data point within a single probplot.
I tried to initialize a cell of character arrays with different colors and used in the following code but didn't work
data =[68391;54744;54682;71629;42610;54371;37500;41222;39767;65042;54706;15108;57000;55460;73360]';
colorarray = cell(1,15);
facecolorarray=cell(1,15);
markertypearray = cell(1,15);
GBIds = false(1,15);
reqd_IDxs = [2 3 5 6 8];
GBIds(reqd_IDxs)=1;
colorarray(GBIds)={'b'};
facecolorarray(GBIds)={'b'};
markertypearray(GBIds)={'o'};
colorarray(~GBIds)={'k'};
facecolorarray(~GBIds)={'r'};
markertypearray(~GBIds)={'+'};
h1=probplot('lognormal',data,'noref');
set(h1(1),'marker',markertypearray,'color',colorarray,'linewidth',3,'markersize',25,'markerfacecolor',facecolorarray);
Error :
Error using matlab.graphics.primitive.Line/set
Error setting property 'Marker' of class 'Line':
Invalid enum value. Use one of these values: '+' | 'o' | '*' | '.' | 'x' | 'square' | 'diamond' | 'v' | '^' | '>' | '<' | 'pentagram' |
'hexagram' | 'none'.
I believe you may not be able to pass a marker array at once and have to pass individual markers. For example, use
set(h1(1),'marker',markertypearray{1},'color',colorarray{1},'linewidth',3,...
'markersize',25,'markerfacecolor',facecolorarray{1});
In order to use different markers, you may need to plot each set of points with the same marker one at a time with hold on, inside a for loop, something like this.

Import a variable from .mat and export to CSV

I have a .mat file which contains a struct (called wiki),
in which there is a field called full_path containing data as follows:
ans =
Columns 1 through 4
{'17/10000217_198…'} {'48/10000548_192…'} {'12/100012_1948-…'} {'65/10001965_193…'}
Columns 5 through 8
{'16/10002116_197…'} {'02/10002702_196…'} {'41/10003541_193…'} {'39/100039_1904-…'}
and so on
How can I create a .csv file with the data present in the curly braces?
This is quite a common problem, which requires very basic functions:
wiki = struct2array(load('wiki.mat', 'wiki'));
fid = fopen('q52688399.csv', 'w');
fprintf(fid,'%s\n', wiki.full_path{:});
fclose(fid);
The above will produce a ~2MB text tile containing a single column of strings.

Combine columns row by row in Matlab

I have a cell array 1300x6 that is all numbers. Below is an example of one row:
| 000 | 00 | 00 | 12 | 345 | 678 |
What I want to accomplish, is to have all the numbers in each row concatenated into one cell so that it is a 1300x1 array. I have tried cat, vertcat, horzcat and reshape but they all just merge the columns into each other, creating more rows. I would like it to look like this:
| 000000012345678 |
Is this possible?
I agree with AnderBiguri that it is strange you would have 000 as a value in a numeric matrix, but you say it is all numbers, so let's go with that.
A = randi(255,[1300,6],'uint8'); %numbers
B = num2str(A); %characters with spaces
for ct = 1:size(B,1),C{ct,1}=strrep(B(ct,:),' ','');end
C %characters without spaces

Produce table with text and number with fprintf in Matlab

I need to produce a table whose first 2 columns have text, and the remaining 2 have numbers. Something like this:
| Ford | Mustang | 1975 | 35 |
| Chev | Camaro | 1976 | 38 |
I have the string in a cell, and the numeric variables in a matrix. I've tried with fprintf but can't make it work. I have no problems doing it in xlswrite, but I don't want to go that way. Any ideas please?
Thanks!
You could use fprintf in a loop like this:
fprintf(1, '| %8s | %8s | %4d | %2d |\n', ...
company{i}, model{i}, year(i), otherNumber(i));
to write to stdout. You can also modify the %#s if you want different spacing in your table, or provide a different file descriptor to the first argument.

howto process non-numeric text file and convert into struct most efficiently?

I have a bunch of .txt files that have the following format:
|file | time | color | tags |
|1 | 1:10 | red | ok, correct|
|2 | 2:20 | blue | bad |
|3 | 1:20 | yellow | sometag |
The first row specifies the column names.
The subsequent rows are 'database' entries.
I want to read in this file, and put all the information into a Matlab structure. I'm wondering what the most efficient way of doing this is.
textread with 'delimiter', '\n' and process each line individually?
textread with 'delimiter', '|' and having to determine which entries belong together?
fread line by line?
I love the convenience of textread with 'delimiter', '\n' but then it's quite a pain to get out the individual entries for each column (with a for loop). Alternatively I can split each row up using regexp:
regexp(file{1}, '\|', 'split')
But this will only split up each row, and won't take care of the whitespaces (for which I would need another regexp call to in order to get rid of).
So what's the most straight forward (and maybe even most efficient) way of doing this?
EDIT1 I want to build a struct like db = struct('file', [], 'time', [], 'color', [], 'tags') which is easy to create once I've read-in the first line.
Let's use textscan:
fid = fopen('asdf.txt');
header = textscan(fid, '%s', 5, 'delimiter', '|');
data=textscan(fid, '|%d %s %s %s','delimiter','|');
fclose(fid);
which gives:
data =
[3x1 double] {3x1 cell} {3x1 cell} {3x1 cell}
From here it's easy to go to the struct you wanted. The strings also have some extra spaces, which need trimming:
data{2} = strtrim(data{2});