Customizing data tips in editor - matlab

This is not exactly a question, because i have the answer, but as I couldn't find sources about it, i'll post the question along with the answer here in case it is of some interest for you. (And also to be able to find it again if i forget about how i did it).
I came across a problem while reviewing some code. The huge amount of variables were not explained in the code and everytime I wanted to know the meaning of a variable I had to look in the Data files and/or guess their meaning from the operations that were done in the code.
No need to say that this was pretty time consuming and i've searched the best way to lose less time.
First of all, I put all the explanations about the variables in comments, the problem being it's adding a (huge!) lot of lines in the script.
I'm also using the "Enable data tips in edit mode" in MATLAB. When you hover over a variable name MATLAB already evaluated, it gives you it's size along all dimensions and at least its first values, making it already easier to know what objects you are manipulating. (see image below)
The natural idea that came to my mind was the following : would it be possible to make MATLAB display some more custom informations in the data tips?
And the answer is yes!
See code in answer

We will need to do some preprocessing in order for this to work, i.e. :
1) Create a data file linking the variable names to their description
(This is the boring part, although i already had to do it in order to understand the code. Just add a line everytime you come across a new variable)
I chose to save these datas in a CSV file, where the first column contains the variable names and the second one contains the descriptions, e.g.
2) Edit MATLAB's function datatipinfo (It's inner code can be accessed by typing edit datatipinfo in MATLAB's command window)
The datatipinfo function looks like :
function datatipinfo(val)
% Some error checking / Initialization
function prefix=sizeType %#ok<DEFNU> All uses are in EVALC calls.
s = size(val);
D = numel(s);
if D == 2
theSize = [num2str(s(1)), 'x', num2str(s(2))];
elseif D == 3
theSize = [num2str(s(1)), 'x', num2str(s(2)), 'x', ...
num2str(s(3))];
else
theSize = [num2str(D) '-D'];
end
if isempty(val) == 0
prefix = [name ': ' theSize ' ' class(val)];
else
prefix = [name ': empty ' theSize ' ' class(val)];
end
end
% Some other stuff
end
And it's the prefix function that we'll edit in order to do what we want to do, along with some filescanning and string comparison in the initialization phase :
A) Initialization phase :
% Read data from csv file :
fid = fopen('ToyVars.csv');
Data = textscan(fid, '%s%s','whitespace','','delimiter',';');
fclose(fid);
B) Compare the name of the variable you're hovering on with the names of the variables in Data
NameFound=0;
% Get Variable Names and Corresponding comments
TmpNames=Data{1,1};
TmpComments=Data{1,2};
% Loop through TmpNames. If a Name matches, assign corresponding comment to the variable Comment
for ii=1:size(TmpNames,1)
if(isequal(char(TmpNames(ii))),name)
Comment=char(TmpComments(ii));
NameFound=1;
end
end
C) Add the comment in the datatip if NameFound==1
if NameFound
if isempty(val) == 0
prefix = [name ': ' theSize ' ' class(val) ' : ' Comment];
else
prefix = [name ': empty ' theSize ' ' class(val) ' : ' Comment];
end
else
if isempty(val) == 0
prefix = [name ': ' theSize ' ' class(val)];
else
prefix = [name ': empty ' theSize ' ' class(val) ];
end
end
And voilĂ !

With a slight tweak and some modifications to the calling function you can also utilize #BillBokeey's answer without any external dependencies. By placing a structure array in the same workspace as the variable you are previewing, you can store a string in a field with the same name as your variable and utilize evalin with the existing logic in datatipinfo to get the inputs for #BillBokeey's modifications.
For my test case I am storing my strings in a structure named mydatastrings:
mydatastrings.test = 'Test Variable';
In the body of datatipinfo I have added a try block:
NameFound = 0;
try
Comment = evalin('caller', sprintf('mydatastrings.%s', name));
NameFound = 1;
end
Along with #BillBokey's modifications to the nested prefix function:
if NameFound
if isempty(val) == 0
prefix = [name ': ' theSize ' ' class(val) ' : ' Comment];
else
prefix = [name ': empty ' theSize ' ' class(val) ' : ' Comment];
end
else
if isempty(val) == 0
prefix = [name ': ' theSize ' ' class(val)];
else
prefix = [name ': empty ' theSize ' ' class(val) ];
end
end
And we achieve the same results.
Full files are in this Gist
EDIT:
You can also make a minimal adjustment to datatipinfo to display a comment without modifying prefix. It seems like whatever executes datatipinfo also captures all of the outputs to the command window and displays them in the popup rather than the command window itself.
If we replace the previous try block with simply:
try
Comment = evalin('caller', sprintf('mydatastrings.%s', name))
end
And leave prefix as it is in the default MATLAB install, we obtain the following popup:
This is also included in the Gist as datatipinfo_noprefix.m

Another approach is to subclass a Matlab's already defined class such as double and add a string property. This string will appear on the data tip.
How to subclass with additional property:
http://fr.mathworks.com/help/matlab/matlab_oop/built-in-subclasses-with-properties.html
The result:

Related

Matlab: How to print " ' " character

I am trying to create the following string:
javaaddpath ('C:\MatlabUserLib\ParforProgMonv2')
However, I could only do the following
command = sprintf('%s ', varargin{1}, '(', varargin{2}, ')');
and that gives me:
javaaddpath ( C:\MatlabUserLib\ParforProgMonv2 )
UPDATE:
Based on Dan's suggestion, I used the following:
command = sprintf('%s', varargin{1}, '(', '''', varargin{2}, '''', ')')
Use two single quotation marks. See the docs for formatting strings, btw this concept is known as an escape character (to help you google such things in the future).
command = sprintf('%s ', varargin{1}, '(''', varargin{2}, ''')')
Although I think you might prefer
command = sprintf('%s (''%s'')', varargin{1}, varargin{2})
or if you have no other varargins (which I guess is very unlikely but anyway)
command = sprintf('%s (''%s'')', varargin{:})
There are a couple of ways around this. First you could declare your path as a string variable then pass the string to your command, eg,
path = 'my/path'
javaaddpath (path)
Or you can use special characters to insert things like a single quote or a new line character, so for a single quote,
EDIT: wrong display command as pointed out by Dan below
myString = '" Hi there! "'
disp(myString)

How to get rid of the punctuation? and check the spelling error

eliminate punctuation
words split when meeting new line and space, then store in array
check the text file got error or not with the function of checkSpelling.m file
sum up the total number of error in that article
no suggestion is assumed to be no error, then return -1
sum of error>20, return 1
sum of error<=20, return -1
I would like to check spelling error of certain paragraph, I face the problem to get rid of the punctuation. It may have problem to the other reason, it return me the error as below:
My data2 file is :
checkSpelling.m
function suggestion = checkSpelling(word)
h = actxserver('word.application');
h.Document.Add;
correct = h.CheckSpelling(word);
if correct
suggestion = []; %return empty if spelled correctly
else
%If incorrect and there are suggestions, return them in a cell array
if h.GetSpellingSuggestions(word).count > 0
count = h.GetSpellingSuggestions(word).count;
for i = 1:count
suggestion{i} = h.GetSpellingSuggestions(word).Item(i).get('name');
end
else
%If incorrect but there are no suggestions, return this:
suggestion = 'no suggestion';
end
end
%Quit Word to release the server
h.Quit
f19.m
for i = 1:1
data2=fopen(strcat('DATA\PRE-PROCESS_DATA\F19\',int2str(i),'.txt'),'r')
CharData = fread(data2, '*char')'; %read text file and store data in CharData
fclose(data2);
word_punctuation=regexprep(CharData,'[`~!##$%^&*()-_=+[{]}\|;:\''<,>.?/','')
word_newLine = regexp(word_punctuation, '\n', 'split')
word = regexp(word_newLine, ' ', 'split')
[sizeData b] = size(word)
suggestion = cellfun(#checkSpelling, word, 'UniformOutput', 0)
A19(i)=sum(~cellfun(#isempty,suggestion))
feature19(A19(i)>=20)=1
feature19(A19(i)<20)=-1
end
Substitute your regexprep call to
word_punctuation=regexprep(CharData,'\W','\n');
Here \W finds all non-alphanumeric characters (inclulding spaces) that get substituted with the newline.
Then
word = regexp(word_punctuation, '\n', 'split');
As you can see you don't need to split by space (see above). But you can remove the empty cells:
word(cellfun(#isempty,word)) = [];
Everything worked for me. However I have to say that you checkSpelling function is very slow. At every call it has to create an ActiveX server object, add new document, and delete the object after check is done. Consider rewriting the function to accept cell array of strings.
UPDATE
The only problem I see is removing the quote ' character (I'm, don't, etc). You can temporary substitute them with underscore (yes, it's considered alphanumeric) or any sequence of unused characters. Or you can use list of all non-alphanumeric characters to be remove in square brackets instead of \W.
UPDATE 2
Another solution to the 1st UPDATE:
word_punctuation=regexprep(CharData,'[^A-Za-z0-9''_]','\n');

delimiting by a char but not deleting it

I have a text file that looks like this:
(a (bee (cold down)))
if I load it using
c=textscan(fid,'%s');
I get this:
'(a'
'(bee'
'(cold'
'down)))'
What I would like to get is:
'('
'a'
'('
'bee'
'('
'cold'
'down'
')'
')'
')'
I know I can delimit with '(' and ')' by specifying 'Delimiter' in textscan, but then I will loose this character, which I want to keep.
Thank you in Advance.
The %s specifier indicates that you want Strings, what you want is individual chars. Use %c instead .
c=textscan(fid,'%c');
Update if you want too keep your words intact then you'll want to load your text using the %s specifier. After the text is loaded you can either solve this problem with Regular Expressions (not my forte) or write your own parser then parses each word individually and saves the paranthesis and words to a new cell array.
AFAIK, there is no canned routine capable of preserving arbitrary delimiters.
You'd have to do it yourself:
string = '(a (bee (cold down)))';
bo = string == '(';
bc = string == ')';
sp = string == ' ';
output = cell(nnz(bo|bc|sp)+1,1);
j = 1;
for ii = 1:numel(string)
if bo(ii)
output{j} = '(';
j = j + 1;
elseif bc(ii)
output{j} = ')';
j = j + 1;
elseif sp(ii)
j = j + 1;
else
output{j} = [output{j} string(ii)];
end
end
Which can probably be improved -- the growing character array will prevent the loop from being JIT'ed. The array bc | bo | sp holds all the information to vectorize this thing, I just don't see how at this hour...
Nevertheless, it should give you a place to start.
Matlab has a strtok function similar to C. Its format is:
token = strtok(str)
token = strtok(str, delimiter)
[token, remain] = strtok('str', ...)
there is also a string replace function strrep:
modifiedStr = strrep(origStr, oldSubstr, newSubstr)
What I would do is modify the original string with strrep to add in delimiters, then use strtok. Since you already scanned the string into c:
c = (c,'(','( '); %Add a space after each open paren
c = (c,')',' ) '); % Add a space before and after each close paren
token = zeros(10); preallocate for speed
i = 2;
[token(1), remain] = strtok(c, ' ');
while(remain)
[token(i), remain] = strtok(c, ' ');
i =i + 1;
end
gives you the linear token array of each of the string you requested.
strtok reference: http://www.mathworks.com/help/techdoc/ref/strtok.html
strrep reference: http://www.mathworks.com/help/techdoc/ref/strrep.html

Saving file with part of file name and date

Hi there I'm currently trying to find a way to save 2 variables from my workspace into a file. I want the file name to be put together using the original and the current date.
I only want the max value from the variables so:
max(streaking)
and
max(tap_total)
The original file name is:
3_FM001_02_05460$BandP$64_24000_FWD_1x1_PRI_PRI_PRI_PRI_15_17_ActivePixelMeans.csv
The only portion of this original file name that I would like to use is:
3_FM001_02_05460$BandP$64_24000_FWD_1x1
These can be saved in a text file or spreadsheet, it doesnt matter.
An example of the new file name would be something like this:
3_FM001_02_05460$BandP$64_24000_FWD_1x1_7-26-2012
Also,
If something could be done in the file to display which variable is which, for example:
Streaking: 1.272 % this would come from the variable max(streaking)
Tap_Total: 2.252 % this would come from the varaible max(tap_total)
EDIT:
% Construct a questdlg with three options
choice = questdlg('Would you like to save?', ...
'Save Options', ...
'Yes','No','Cancel','Cancel');
% Handle response
switch choice
case 'Yes'
disp([choice ' processing.'])
save_option = 1;
case 'No'
disp([choice ' processing.'])
save_option = 0;
case 'Cancel'
disp('Canceled.')
save_option = 2;
end
file_to_get = evalin( 'base', 'file_to_get' );
streaking = evalin( 'base', 'streaking' );
tap_total = evalin( 'base', 'tap_total' );
if save_option == 0
elseif save_option == 1
max_streak = max(streaking);
max_tap = max(tap_total);
str_streak = mat2str(max_streak);
str_tap = mat2str(max_tap);
fname = file_to_get;
pruned_fname = regexprep(fname,'_PRI(\w*).(\w*)','');
new_fname = [pruned_fname '_' date '.csv'];
path1 = '\\pfile01thn\bbruffey$\My Documents\analysis data\Banding and Streaking Results';
fid = fopen([path1 new_fname], 'w');
fprintf(fid,['Max Banding: %s\nMax Streaking: %s'],str_tap,str_streak)
fclose(fid);
elseif save_option == 2
end
This would be a great place to use the regexprep command to prune the original filename down.
Example:
fname = '3_FM001_02_05460$BandP$64_24000_FWD_1x1_PRI_PRI_PRI_PRI_15_17_ActivePixelMeans.csv';
pruned_fname = regexprep(fname,'_PRI(\w*).(\w*)','');
pruned_fname =
3_FM001_02_05460$BandP$64_24000_FWD_1x1
Now, a note about the regexprep command I used here. This is specific for the filename you provided here. Since it looks like you want to trim off everything after the the first _PRI I used a tag (\w*) which means any combination of [a-z A-Z _ 0-9] can follow. Notice that since this is a filename there is a . there hence why I added a period in and followed that with another (\w*) to finish out the csv. You can find more info on these sorts of operators here.
Now that you have the pruned_fname you can simply add whatever you want to it. So if you want to add the date in with an underscore to space it just simply do something like this:
new_fname = [pruned_fname '_' date '.csv'];
new_fname =
3_FM001_02_05460$BandP$64_24000_FWD_1x1csv_26-Jul-2012.csv
Now you simply need to open the file to write to it. you might need to append the path to where you want to save it, I'm just going to call it path for now. It would be something like C:\Documents\
fid = fopen([path new_fname], 'w')
Now with the fid you have the id of the file you want to write to. This should be a new file and if it isn't you are going to overwrite the file contents if you do it this way. Just be aware =)
Next you can simply write those first two lines to the file using fwrite fprintf, just to name a few possible functions.
Hopefully that'll get you setup there!

How to compare the special characters such as ' (' , '/', ' ' in Matlab

I have a character array, cA, and each entry of each stores a single character, such as 'a', '(', '[', and ' '.
My program will iterate this array and perform a certain type of operation based on the content of each entry. For example,
if (cA(i) == 'a') do sth; end
For the characters such as 'a' , 'b', the above character comparison operation is trivial.
But how can I handle the special characters such as '(', '[' and ' '( a blank space)
Can I write something such as
if (cA(i) == ' ')
if (cA(i) == '(')
In general, I suggest comparing strings using STRCMP, e.g.
if strcmp(cA(i),'('), doSomething, end
For your specific case, I suggest a SWITCH/CASE statement. Note that in Matlab, this doesn't fall through like in c; instead, only the "good" case gets executed. In my experience, this helps readability. Thus
for myChar = cA %# this loops over every char in the 1-by-n string
switch myChar
case 'a'
do something
case 'b'
case {'c','d'} %# this is how you handle multiple chars with the same outcome
otherwise
error('char %s not handled',myChar) %# don't forget this
end
end
Did you try it and get an error? Because it works for me:
c = '(';
if(c == '(')
disp('left paren')
end
This prints left paren, as expected.