Produce table with text and number with fprintf in Matlab - 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.

Related

Understanding ibase and obase used

I'm trying to solve the following exercise:
Write a command line that takes numbers from variables FT_NBR1, in ’\"?! base, and FT_NBR2, in mrdoc base, and displays the sum of both in gtaio luSnemf base.
I know the solution is:
echo $FT_NBR1 + $FT_NBR2 | sed 's/\\/1/g' | sed 's/?/3/g' | sed 's/!/4/g' | sed "s/\'/0/g" | sed "s/\"/2/g" | tr "mrdoc" "01234" | xargs echo "ibase=5; obase=23;" | bc | tr "0123456789ABC" "gtaio luSnemf"
I don't understand why ibase=5 and obase=23.
I read about ibase and obase, and I understand this is a base conversion, from base 5 to base 23. Anyone can explain me why 5 and 23. Thank you
The exercise description is a bit weird. A better one would be
Write a command line that takes numbers from variables FT_NBR1, with numbers represented by the letters "’\"?!", and FT_NBR2, represented by "mrdoc", and displays the sum of both with numbers represented by "gtaio luSnemf".
A shorter answer would be
echo $FT_NBR1 + $FT_NBR2 | tr "\'\\\\\"\?" "01234" | tr "mrdoc" "01234" | xargs echo "ibase=5; obase=23;" | bc | tr "0123456789ABC" "gtaio luSnemf"
Let's take it from the beginning:
echo $FT_NBR1 + $FT_NBR2 creates the expression using the input strings
tr "\'\\\\\"\?" "01234" translates the first input alphabet into numbers
tr "mrdoc" "01234" translates the second input alphabet into numbers
xargs echo "ibase=5; obase=23;" prepends number base information; the input base is 5 and the output base is 13, but obase must be expressed in the base of ibase and 13 in base 5 is 23.
bc does the actual calculation
tr "0123456789ABC" "gtaio luSnemf" does the translation into the output alphabet.

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.

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

Read from .mat file

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

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});