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
Related
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.
Ok, coming from Python and never having used MATLAB before, it seems like it is unnecessarily hard to write data to a csv using MATLAB...
So my data looks like this:
col1 A2A B2 CC3 D5
asd189 123 33 71119 18291
as33d 1311 31 NaN 1011
asd189 NaN 44 79 191
It has N header columns that are made of alphanumeric strings.
It has a leftmost column of length M which is made of alphanumeric strings.
It has an (M-1) x (N-1) array of NUMERIC data, with possible NaNs.
Can you please provide code to write this to a csv? I cannot use the xlswrite function because I'm on a cluster without Excel installed. Really just want to get on with the actual data analysis. Thanks
You can only write matrices (not cell arrays) directly using csvwrite, and as you say you need Excel installed for xlswrite, so that leaves you with low level operations. You can see a walkthrough for writing to text files here, and code for your example below:
% Initialise example cell array
M = {'col1', 'A2A', 'B2', 'CC3', 'D5'
'asd189', 123, 33, 71119, 18291
'as33d', 1311, 31, NaN, 1011
'asd189', NaN, 44, 79, 191};
% Open a file for writing to (doesn't have to already exist, can specify full directory)
fID = fopen('test.csv','w');
% Write header line, formatted as strings with comma delimiter. Note \r\n for new line
fprintf(fID, [repmat('%s, ', 1, size(M,2)-1),'%s\r\n'], M{1,:});
% Loop through other rows
for row = 2:size(M,1)
% Write each line of cell array, with first column formatted as string
% and other columns formatted as floats
fprintf(fID, ['%s, ', repmat('%f, ', 1, size(M,2)-2),'%f\r\n'], M{row,:});
end
% Close file after writing
fclose(fID);
Result:
Use writetable. It makes writing to CSV (or to an Excel file, or to other text-delimited file formats) much easier than using csvwrite, or xlswrite, or low-level commands such as fprintf.
>> t = table({'asd189';'as33d';'asd189'},[123;1311;NaN],[33;31;44],[71119;NaN;79],[18291;1011;191]);
>> t.Properties.VariableNames = {'col1','A2A','B2','CC3','D5'}
t =
col1 A2A B2 CC3 D5
________ ____ __ _____ _____
'asd189' 123 33 71119 18291
'as33d' 1311 31 NaN 1011
'asd189' NaN 44 79 191
>> writetable(t,'myfile.csv')
If your data is currently not stored as a table (maybe it's in an array or cell array), it's pretty easy to convert to a table using utility functions such as array2table or cell2table. You will only pay a small time penalty for doing this.
PS - you don't need Excel to be installed in order to write to an Excel file. You may not be able to read them afterwards, but MATLAB can still write them. But it sounds like you'd prefer .csv anyway.
I have this kind of data:
label-> 1 2 3 4 5
val1 1.67E+07 2.20E+07 3.04E+07 7.89E+07 1.24E+08
val2 1.71E+07 2.35E+07 2.70E+07 7.80E+07 1.31E+08
val3 1.48E+07 2.15E+07 2.74E+07 7.18E+07 1.17E+08
val4 1.57E+07 2.07E+07 2.49E+07 7.46E+07 1.27E+08
val5 1.32E+07 2.23E+07 3.07E+07 7.50E+07 1.16E+08
I need to plot the label vs the average of each val column, like this:
label-> 1 2 3 4 5
val1 1.67E+07 2.20E+07 3.04E+07 7.89E+07 1.24E+08
val2 1.71E+07 2.35E+07 2.70E+07 7.80E+07 1.31E+08
val3 1.48E+07 2.15E+07 2.74E+07 7.18E+07 1.17E+08
val4 1.57E+07 2.07E+07 2.49E+07 7.46E+07 1.27E+08
val5 1.32E+07 2.23E+07 3.07E+07 7.50E+07 1.16E+08
mean 1.55E+07 2.20E+07 2.81E+07 7.57E+07 1.23E+08
Is there any possibility of perform this operation in gnuplot or should I keep attached to Excel?
You could do it using awk and gnuplot. Assume your example data (without mean row) is in data.txt.
Then you could calculate the mean in each column starting from the second column (from i=2) and the second row (record, or row, #1 -- NR==1 -> do not summate, but fill auxiliary array a with zeroes: a[i]=0.0). For that purpose one could use awk condition: if (NR==1)... else {...calculate the means...}.
Awk reads the data row-by-row. In each row, you iterate over fields and summate the data from column with number i into array element a[i]:
{for(i=2;i<=NF;i++) a[i]+=$i;}
When iterating over the first row (NR==1), we would ;
At the END of awk script (all rows processed), just divide by number of columns in your data NF-1 to calculate the mean values. Note, the code below assumes you have rectangular-formatted data (NF=const).
Also, save row column labels into label array:
if (NR==1) {for(i=2;i<=NF;i++) label[i]=$i; ... }
Then print the labels and mean values into the rows, one row for one label.
for(i=2;i<=NF;i++) {printf label[i]" "; print a[i]/(NF-1)}
The final data table would look that way:
1 15500000
2 22000000
3 28080000
4 75660000
5 123000000
Then you could plot one column against the other.
Note, the final data for gnuplot should be formatted in columns, not rows.
The following code performs the described operations:
gnuplot> unset key
gnuplot> plot "<export LC_NUMERIC=C; awk '{if (NR==1) {for(i=2;i<=NF;i++) label[i]=$i; a[i]=0.0;} else {for(i=2;i<=NF;i++) a[i]+=$i;};} END {for(i=2;i<=NF;i++) {printf label[i]\" \"; print a[i]/(NF-1)}};' data.txt"
Note, that spaces should be escaped with backslash character \ in the gnuplot.
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.
How can I convert the numbers in the range 1 through 26 to their respective letter position in the alphabet?
1 = A
2 = B
...
26 = Z
CHR(#) will give you the ASCII character, you just need to offset it based on the ASCII table:
e.g. A = 65, so you will need to add 64 to 1:
CHR(64 + #) = A if # is 1
ASCII code is the numerical representation of a character such as 'a' or 'Z'. Therefore by looking at the table one can see that capital A has a value of 65 and Z has a value of 90. Adding 64 from each value in the range 1-26 will give you their corresponding letter.