I want to print the following text on a matlab figure with ':' sign vertically aligned:
Total Events: 1234
Mean Value: 1234
Standard Deviation: 1234
1st Quartile: 1234
...
...
I am using text function and trying to insert spaces manually but they never seemed to be aligned perfectly and looks wavy.
Any helps, please.
You can use a fixed-width font and use a cell array to put the text on different lines:
text( 0, 0, { ' Total Events: 1234' ...
' Mean Value: 1234' ...
'Standard Deviation: 1234' ...
' 1st Quartile: 1234' }, 'FontName', 'FixedWidth' )
Use string format
strings = {'Total Events', 'Mean Value', 'Standard Deviation', '1st Quartile'};
max_len = max( cellfun( #numel, strings ) );
fs = sprintf( '%% %ds: 1234\n', max_len );
cellfun( #(x) fprintf(1,fs,x), strings );
Results with:
Total Events: 1234
Mean Value: 1234
Standard Deviation: 1234
1st Quartile: 1234
As you can see the command that aligns to the right is
sprintf( '% 18s: 1234', strings{1} )
So, eventually in your text command, instead of explicitly writing a string, you can format one using sprintf:
text( 0, 0, sprintf( '% 18s: %d', strings{ii}, values(ii) );
Assuming you have strings - cell array of strings and same-length array values with corresponding values to print.
Related
Can somebody explain me how i get some specific values after the = sign? The input File is a .subvar file format.
I dont know how to jump in the right row and column to get the value. Do you have a matlab tutorial link for such a problem.
I need for example two specific values (after the = sign):
The value of $_Wk1_lr_m and $_Wk1_voll_m
!file.version=1.543!
! Testautomatisch
subvargroup.begin ($G_Wk1)
subvar( $_Wk1_lr_C_x, str = ' 0.019 ' )
subvar( $_Wk1_lr_m, str = ' 15601 ' ) ! [kg] lr
subvar( $_Wk1_lr_C_y, str = '-0.007 ' )
subvar( $_Wk1_lr_C_z, str = ' 1.644 ' )
subvar( $_Wk1_voll_m, str = ' 33690 ' ) ! [kg] voll
subvargroup.end ($G_Wk1)
What are the first steps to get the right row and the right column? Thank you and stay at home :)
read the file line by line, match the line format and extract the values using regular expression regexp
fid=fopen('mydata.subvars','rt');
res=struct;
while(~feof(fid))
line=fgetl(fid);
if(regexp(line,'^\s*subvar\(','once'))
val=regexp(line,'\$_(\w+),\s*str\s*=\s*''\s*([0-9.-]+)\s*','tokens');
if(length([val{:}])==2)
res.(val{1}{1})=str2num(val{1}{2});
end
end
end
fclose(fid);
here is the result
>> res
res =
Wk1_lr_C_x: 0.0190
Wk1_lr_m: 15601
Wk1_lr_C_y: -0.0070
Wk1_lr_C_z: 1.6440
Wk1_voll_m: 33690
I have some data which look like this:
I would like to pre-process the data in a way that I replace all Mostly false with 1, Mostly true with 2 and Definitely true w/ 3. Is there a find and replace command or what is the best way of doing this?
You can use a map object to do the mapping
m = containers.Map( {'Mostly false', 'Mostly true', 'Definitely true'}, ...
{ 1, 2, 3} );
Then for some example data
data = {'Mostly false', 'Mostly false', 'Mostly true', 'Mostly false', 'Definitely true'};
You can perform the conversion with
data = m.values( data );
% >> data = {1, 1, 2, 1, 3}
This assumes there will always be a match in your map.
Alternatively, you could do the operation manually (for the same example data), this will leave non-matches unaltered, and you could use strcmpi for case-insensitivity:
c = {'Mostly false', 'Mostly true', 'Definitely true'
1, 2, 3};
for ii = 1:size(c,2)
% Make the replacement for each column in 'c'
data( strcmp( data, c{1,ii} ) ) = c(2,ii);
end
I'm working with sscanf to extract a number from a string. The strings are usually in the form of:
'44 ppm'
'10 gallons'
'23.4 inches'
but ocassionally they are in the form of:
'<1 ppm'
If I use the following code:
x = sscanf('1 ppm','%f')
I get an output of
1
But if I add the less than sign in front of the one:
x = sscanf('<1 ppm','%f')
I get:
[]
How can I write this code so this actually produces a number? I'm not sure yet what number it should print...but let's just say it should print 1 for the moment.
You can use regexp:
s= '<1 ppm';
x=regexp(s, '.*?(\d+(\.\d+)*)', 'tokens' )
x{1}
Demo :
>> s= {'44 ppm', '10 gallons', '23.4 inches', '<1 ppm' } ;
>> x = regexp(s, '.*?(\d+(\.\d+)*)', 'tokens' );
>> cellfun( #(x) disp(x{1}), x ) % Demo for all
'44'
'10'
'23.4'
'1'
I'm trying to use fprintf to write a data file. Say I have a matrix containing data like this:
Values = [ 1, 735123.0, 23, 24, 25;
2, 735123.5, 34, 35, 36;
...
8000, 739122.5, 21, 22, 43]
I can write that to a file using:
fprintf(FileID, '%d, %f, %d, %d, %d', Values')
But really that second column represents dates (datestr(735123.5) = 11-Sep-2012 12:00:00) and I'd like fprintf to print dates in the second column of the data file, so that the file will read
1, 11-Sep-2012 00:00:00, 23, 24, 25
2, 11-Sep-2012 12:00:00, 34, 35, 36
...
8000, 24-Aug-2023 12:00:00, 21, 22, 43
My matrix is thousands of lines long so I'd prefer not to have to loop through line by line.
Any suggestions how to procede?
Something like
n = num2cell(Values');
n(2,:) = cellfun(#datestr, n(2, :),'UniformOutput', false);
fprintf(FileID, '%d, %s, %d, %d, %d', n{:});
Although cellfun is basically a loop.
Without for loops or cellfun:
separator = repmat(', ',size(Values,1),1); %// column used as separator
lineFeeds = repmat('\n',size(Values,1),1); %// column of line feeds
string = [ num2str((Values(:,1))) ...
separator ...
datestr(Values(:,2)) ...
separator ...
num2str(Values(:,3)) ...
separator ...
num2str(Values(:,4)) ...
separator ...
num2str(Values(:,5)) ...
lineFeeds ];
string = reshape(string.',1,[]); %'// put everything in one row
FileID = fopen('tmp.txt', 'wt'); %// use 't', or '\n' may not get written to file
fprintf(FileID, string);
fclose(FileID);
I have 100 ASCII files in my directory all named as follows:
int_001.ASC
int_002.ASC
int_003.ASC
.
.
.
int_099.ASC
int_100.ASC
I have to import them in MATLAB all with importdata, which should work as follows:
A = importdata('int_001.ASC', ' ', 9)
x = A.data(:,1)
y = A.data(:,2)
My question is: how can I avoid writing 100 times importdata? Is there a way to write the first string only and then have all data uploaded?
Thanks
fls = dir( 'int_*.ASC' );
for fi=1:numel(fls)
A{fi} = importdata( fls(fi).name, ' ', 9 );
% ...
end
UPDATE:
You may use string formatting to read the files according to their numbers:
for fi=1:100
A{fi} = importdata( sprintf('int_%03d.ASC', fi ), ' ', 9 );
% ...
end
You can use strcat function in a for loop :
for k=1:n
fileName = strcat('int_',num2str(k, '%03d'),'.ASC');
A(k) = importdata(fileName, ' ', 9);
x(k) = A(k).data(:,1);
y(k) = A(k).data(:,2);
end
If you want to take this a little overboard:
alldata = arrayfun(...
#(dirEntry)importdata(dirEntry.name, ' ', 9), ...
dir('int_*.ASC'),...
'uniformoutput',false);
This line does the following
Gets a listing of all files matching the partial filename, as an array of structures (h/t Shai)
For each element in that array, performs the importdata call from your original post.
Compiles all the outputs into a cell array.