I'm working on a MATLAB program with a gui. I want to have text labels and buttons in french, but it doesn't work. For example, the word 'Paramètres' in the code becomes Paramètres on the gui.
I checked the file encoding and it's utf-8. What can I do to fix that?
Here's a simple example of one command that I used in the code:
tab2 = uitab('v0', hTabGroup, 'title','Paramètres des canaux');
Thanks.
How about using HTML?:
figure
hTabGroup = uitabgroup;
drawnow;
tab2 = uitab('v0',hTabGroup,'title','<html>Paramètres des canaux</html>');
See here for a list of HTML character codes.
To add an accent aigu use
title('{Param\''etres des canaux}','interpreter','latex')
To add an accent grave use
title('{Param\`etres des canaux}','interpreter','latex')
I found the answer on this stackoverflow page. Basically, I just have to set MATLABencoding to UTF-8 before creating the GUI. The command is simply:
feature('DefaultCharacterSet','UTF-8');
and that's it!
I was having difficulty copy-pasting the string from SO to MATLAB, as the "è" showed up as char(65533) (instead of the correct char(232)) for some reason...
Anyway, I threw together a small conversion utility to convert strings or cellstrings to their Unicode-in-HTML equivalent, to complement horchler's answer:
function html = toHTML(strings)
%% Initialize
% Basic IO check
if ~iscellstr(strings) && ~ischar(strings)
error(...
'toHTML:invalid_input',...
['Invalid input class: ''%s''.\n',...
'Supported input types are ''char'' or a ''cell'' containing ''char''.'], class(strings));
end
% Provide support for
% - Single and multiline line char arrays
% - Cellstrings
wasChar = ischar(strings);
if wasChar
if size(strings,1) > 1
strings(:, end+1) = char(10);
end
strings = {strings};
end
%% Convert all strings to their unicode representation in HTML
% Just for abbreviation
uf = {'UniformOutput',false};
% Convert all characters to their HTML unicode representation
html = cellfun(#transpose, strings, uf{:});
html = cellfun(#(x) cellstr(num2str(x(:)+0)), html, uf{:});
html = cellfun(#(x) cellfun(#(y) ['&#' strtrim(y) ';'],x, uf{:}), html, uf{:});
% Include HTML tags
html = cellfun(#(x) ['<html>' [x{:}] '</html>'], html, uf{:});
% Take care of newlining
html = regexprep(html, '
', '<br>');
html = regexprep(html, '<br></html>$', '</html>');
% Make output type consistent with input type
if wasChar
html = [html{:}];
end
end
I'm currently submitting this to the FEX as well. If anyone knows whether such a thing exists already, please let me know.
I've fixed this problem with this technique.
Write this in your terminal :
export LC_CTYPE="en_US.ISO-8859-1"
Then, launch Matlab and try :
title('été');
if that works, you only need to create a script that will execute the export command before launching Matlab. Either in your .bashrc file, a custom script launching Matlab, etc...
My workaround is simply to create a custom script (i.e. "mat" in my /home/username/bin directory) :
#!/bin/bash
cd /home/username/Matlab
export LC_CTYPE="en_US.ISO-8859-1"
/path/to/matlab/matlab
Then, I created an alias in the .bashrc file of the /home/username directory to launch the script "mat"
alias m="/home/proy/bin/mat &"
If it's inside a matlab script you store your string with accentuated characters, try to change the encoding of your matlab script to ANSI (eg, with Notepad++ or SublimeText).
Related
My matlab code for dataimport is giving me different results for what appear to be similar text files as input. Input1 gives me a normal cell with all lines from the text file as entries in the cell which i can reference using {i}.
Input2 gives me a scalar data structure where all numeric entries in my text file are converted to the input.data structure. I want all files to be converted to regular cell entries and I do not understand why for some files they are converted to scalar data structures.
Code: input = importdata(strcat(direct,'\',filename));
Input1 example: Correctly working dataimport, with text file on the right
File link: https://drive.google.com/open?id=1aHK4xivqEqJEmaA8Dv8Y0uW5giG-Bbip
Input2 example: Incorrectly working data import, with text file on the right FIle link: https://drive.google.com/open?id=1nzUj_wR1bNXFcGaSLGva6uVsxrk-R5vA
UTSL!
I'm guessing you are using GNU Octave although you are writing "Matlab" as topic of your question.
In importdata.m around line 178, the code tries to automatically detect the delimiter for your data:
delim = regexpi (row, '[-+\d.e*ij ]+([^-+\de.ij])[-+\de*.ij ]','tokens', 'once');
If you run this against W40A0060; you get A as delimiter because there is basically a number before and after it.
If you run this against W39E0016; you get {} as delimiter(empty) because the E could be part of a number in scientific notation and is therefore excluded.
Solution:
you really should add the correct delimiter to the importdata call and not trust that it's magically detected.
And if you just want the lines in a cell, use
strsplit (fileread ("W39E0016_Input2.txt"), "\n")
Analysis
This looks indeed strange!
EDIT: The cause for this strange looking behaviour has been deciphered by #Andy (See his solution).
When you use all outputs of importdata() function you can see what happens when reading the data:
[dat1,del1,headerrows1]=importdata('Input1.txt')
[dat2,del2,headerrows2]=importdata('Input2.txt')
For your first file it recognizes 69 header riws and no delimiter:
del1 = []
headerrows1 = 69
while in your second file only two header rows and a comma , delimiter is recognized
del2 = ','
headerrows2 = 2
I can not find an obvious reason in your files causing this different interpretation of data.
Suggestion
Your data format is rather complex. It is not a simple table like produced from excel. It has multiple lines with a different number of fields per line and varying data types. importdata() is not designed for this type of data. I suggest to write a specific import function for this kind of file. Have a look at textread() for a first guess. You can use it to read the lines of the files as text and later interpret it with sscanf() or use strsplit() to split the line contents into fields.
I am forming an specific string using several strcat and displaying it into console. This string contains characters such as: 1,2,3,4,5,6,7,8,9,0,#,*,E and am using fprintf('%s') for this purpose.
For instance:
2E4137E65922#
is a possible outcome of the code.
Is there anyway I could make letter E to stand out in my output? Like making it red?
Unfortunatedly, there is no official way of doing this. However, you could use Yari Altman's cprintf(). It abuses of undocumented features of Matlab to do exactly what you want.
You can read more in the famous Undocumented Matlab blog he runs.
The example image in FEX looks like this:
EDIT: Theoretically, if cprintf would work as expected, the following should work:
C=strsplit(s,'E');
cprintf('black',C{1});
for ii=2:size(C,2)
cprintf('err','E');
cprintf('black',C{ii});
end
cprintf('black','\n');
However, in Matlab 2014b it doesnt give good results. I found out that of it doesnt work properly when there is a single character to format.
If you substitute 'E' by 'EE' works....
EDIT2: I left a comment to Yari Altman. Hopefully he will, if he can, fix the thing.
You can use the HTML tags <strong>, </strong> to type specific letters in bold:
str = '2E4137E65922#'; %// input string
letter = 'E'; %// letter that should be made bold
strBold = regexprep(str, letter, ['<strong>' letter '</strong>']); %// output string
disp(str)
disp(strBold)
Thanks #Dev -iL for this information!
While it seems that cprinf() from my other answer does not work for single characters, if there is a single color that one wants to use, and that color is orange, then this trick used for warning in cprintf can be used:
disp(['this is [' 8 'orange]' 8 ' text'])
Read more at: http://undocumentedmatlab.com/blog/another-command-window-text-color-hack
Thus, your code would look like:
s='2E4137E65922#';
C=strsplit(s,'E');
str=C{1};
for ii=2:size(C,2)
str=[str ['[' 8 'E]' 8 ]];
str=[str C{ii}];
end
disp(str);
I’m am trying to create a pdf file from matlab figure using cmyk colors, but facing a problem with umlauts and also some other special characters. Is there any other way to handle this than Latex? The following example demonstrates the issue.
plot(rand(199,1))
title_string = ['Some text:äö' char(228) ':2005' char(150) '2008:end text'];
title(title_string);
print(gcf,'-dpdf','cmykfile.pdf','-r600','-cmyk');
print(gcf,'-dpdf','rgbfile.pdf','-r600');
As you can see from the pdf-files the RGB-version handles umlauts, but not en-dash, and CMYK skips them all.
PDF is generated in Matlab using Ghostscript, but I have not found how to configure character encoding for GS.
I am using Windows and Matlab R2014.
I'm not completely sure this is the solution you was looking for.
Anyway, if you create an eps first and then convert it to pdf the output file doesn't have any issue with the special characters in the title, provided that you don't build your title string using char.
plot(rand(199,1))
title_string = 'Some text:äöä:2005—2008æ:end text';
title(title_string);
print(gcf,'-depsc','cmykfile.eps','-r600','-cmyk');
!ps2pdf cmykfile.eps cmykfile.pdf
The code above works if you have the ps2pdf utility in your system path. You already have ps2pdf on your computer if you have MiKTeX installed, but you might need to update your system path. Basically ps2pdf should be a shortcut to gs, therefore also if you have only gs and not MiKTeX installed, you should be able to achieve the same result.
EDIT
On my machine (Windows 7, MATLAB R2014b), also this code works well, without the need to use ps2pdf:
plot(rand(199,1))
title_string = 'Some text:äöä:2005—2008æ:end text';
title(title_string);
print(gcf,'-dpdf','cmykfile.pdf','-r600','-cmyk');
It seems that the issue happens when you build the title string using char.
I'm reading a string like "1.0.2" from text file with these codes :
reader = fopen('Address\My_Text.txt');
Out= textscan(reader,'%str');
Out1=Out{1} ;
Out2=Out1{1};
fclose(reader);
This code (Out2) returns a string like this: 1.0.2 . This is a text file that copied by MATLAB from other place in HDD and read one time with above code for comparing with some existed text file and after that replace with this file using movefile (The main file is working correctly). When I create a text file manually and insert "1.0.2" in it, These codes read this value correctly. What is the problem? What is the solution for MATLAB?
Thanks.
You can use fopen('My_Text.txt', 'r', 'n', 'UTF-8') to open this file in UTF-8 encoding. For the added 3 parameters, check documentation of fopen for details.
Inserting fseek(reader, 3, 'bof') before textscan may also fix this problem, in a different manner.  is the BOM for UTF-8.
I am using the fprintf command to store the contents of a .mat file to a .txt. The .mat file contains strings. My code prints the data in the same column.
fid = fopen('exp.txt','wt');
for i=1:275
fprintf (fid,classes{i}{1})
end
fclose(fid);
When I use the \n and the '\r\n' options, they doesn't print anything to the file. I'd appreciate the help!
Some text editors will show those new line characters and some wont. This happens because of different standards followed by different softwares and operating systems for eg:
end of line sequences
Windows end of line sequence: \r\n
Unix end of line sequence: \n
Mac end of line sequence: \r
So if you really want good human readable formats, either fix your operation system/software and use characters friendly for that system, or if you want uniformity in the reports, better write files in standard HTML formats :)
adding a "br" tag and naming file .html is as simple as writing out '\n' and naming it .txt!