You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to insert fprintf value into GUI edit text box?
15 views (last 30 days)
Show older comments
Hi there! I'm having issues on inserting my fprintf value into the GUI's edit text box...I think the error is at where I marked below but I can't seem to solve it. If you try to remove those two lines of code and just leave there as "fprintf('%s', morse{index});" , you will see my desire output at the command window...
In this case, you will see something similar as mentioned above but the difference is there'll be a random number showing in the GUI's edit text box...can someone help ? thanks!
morseoutput=fprintf('%s', morse{index}); %(I think here's the error)
set(handles.morsecode_output,'string',morseoutput) ;
1 Comment
Stephen23
on 19 Apr 2021
"...there'll be a random number showing in the GUI's edit text box"
Not random: the fprintf documentation states that it returns the number of bytes printed to the command window or file.
Accepted Answer
Stephen23
on 18 Apr 2021
Edited: Stephen23
on 18 Apr 2021
24 Comments
Phoebe
on 19 Apr 2021
hi there! Previously i have tried to change fprintf to sprintf and it works but it's only displaying the last morse code...for example if I insert the word "hi" into my gui, the output I get is just the morse code of "i"...how do i solve this problem? that's also why i used fprintf above because it's showing the full output in my command window
Stephen23
on 19 Apr 2021
Edited: Stephen23
on 19 Apr 2021
The basic problem is that on every loop iteration you replace the results of the previous iteration, in the end leaving you with only the data from the final loop iteration. You could solve this problem by accumulating the required data in one array within the loop, and then calling SET with that data after the loop, something like this:
n = numel(engmsg);
c = cell(1,n);
for k = 1:n
[~,x] = ismember(engmsg(k), letter);
if x
c(k) = morse(x);
end
end
set(handles.morsecode_output,'string',[c{:}]);
However a much better use of MATLAB is to avoid the loop entirely:
msg = 'Hello';
morse = {'-----','.----','..---','...--','....-','.....','-....','--...','---..','----.','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
letter = ['0':'9','A':'Z','_'];
[X,Y] = ismember(upper(msg),letter);
str = sprintf(' %s',morse{Y(X)})
str = ' .... . .-.. .-.. ---'
Printing to the command window using fprintf is not a solution.
Phoebe
on 19 Apr 2021
Edited: Phoebe
on 19 Apr 2021
I tried modifying the first example on my own codes, It works like a charm!! however,when I tried the second example (because it's less complicated) the output cannot be shown in the gui, and also it's only showing one morse code output in the command window. I'm quite new to matlab so it's actually difficult for me to digest the first example
Stephen23
on 19 Apr 2021
"when I tried the second example (because it's less complicated) the output cannot be shown in the gui, and also it's only showing one morse code output in the command window"
You need to set the string property using str:
set(handles.morsecode_output,'string',str);
Phoebe
on 19 Apr 2021
can someone explain why using this won't work?
letter={'1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','_'};
Walter Roberson
on 19 Apr 2021
ismember('cab', 'a':'d')
ans = 1×3 logical array
1 1 1
ismember('cab', {'a', 'b', 'c', 'd'})
ans = logical
0
When you use {} for the second parameter, then the comparison used is string comparison: the right hand side would be scanned looking for places that were 'cab', such as
ismember('cab', {'taxi', 'truck', 'cab', 'suv'})
ans = logical
1
Phoebe
on 19 Apr 2021
one last question, if i were to do something vice versa why it is only showing one output on my gui?
morse = {'-----','.----','..---','...--','....-','.....','-....','--...','---..','----.','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
letter = ['0':'9','A':'Z','_'];
[X,Y] = ismember(morsemsg,morse);
engoutput = sprintf(' %s',letter(Y(X)))
set(handles.engmsg_input,'string',engoutput);
Walter Roberson
on 19 Apr 2021
handles.engmsg_input = uicontrol('style', 'text', 'Position', [0 0 100 100]);
engmsg = ' .... . .-.. .-.. ---'
morsemsg = strsplit(engmsg, ' ');
morsemsg(cellfun(@isempty, morsemsg)) = []
morse = {'-----','.----','..---','...--','....-','.....','-....','--...','---..','----.','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
letter = ['0':'9','A':'Z','_'];
[X,Y] = ismember(morsemsg,morse);
engoutput = letter(Y(X))
set(handles.engmsg_input,'string',engoutput);
works for me to show 'HELLO'
It should only be showing one output.
If you are wanting input of / to indicate start of a new line in output then you need to program that:
handles.engmsg_input = uicontrol('style', 'text', 'Position', [0 0 100 100], 'max', 2);
engmsg = ' .... . .-.. / .-.. ---'
morsemsg = strsplit(engmsg, ' ');
morsemsg(cellfun(@isempty, morsemsg)) = []
morse = {'-----','.----','..---','...--','....-','.....','-....','--...','---..','----.','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
letter = ['0':'9','A':'Z','_'];
[X,Y] = ismember(morsemsg,morse);
engoutput = strsplit( letter(Y(X)), '_')
set(handles.engmsg_input,'string',engoutput);
Phoebe
on 19 Apr 2021
I think mine doesn't work because I didn't do the second line of code...can you pls explain these codes to me?
morsemsg = strsplit(engmsg, ' '); % this is to spilt up every morse right?
morsemsg(cellfun(@isempty, morsemsg)) = [] %i don't quite understand this one but it feels like you are trying to fit them into an array
Walter Roberson
on 19 Apr 2021
You have a single string of morse input that has spaces in it. The strsplit() breaks it up into pieces at the spaces.
In the case that you had extra spaces in the morse input, such as a space at the beginning or at the end, or two or more spaces in a row in the middle, then strsplit() will end up creating empty entries. For example,
... --- ...
^^ ^ ^
where the marked ^^ place has two spaces instead of one space and the string ends in a space. strsplit applied to that would not automatically be smart enough to know that you want {'...', '---', '...'} as your pieces, and will instead create {'...', '', '---', '...', ''} where the '' are empty character vectors.
The cellfun(@isempty, morsemsg) finds the empty entries (if any are there are all) returning a logical vector for each entry as to whether it is empty or not. Then the indexing of morsemsg by that logical vector selects the ones that are empty, and the assignment of [] deletes the entries. So afterwards, morsemsg will not have any of those '' entries that corresponded to unexpected spaces.
Phoebe
on 23 Apr 2021
Edited: Phoebe
on 23 Apr 2021
greetings, sorry to interrupt again, I want to come up with an error dialog but I don't know how to write the code for if engmsg isn't equivalent to the items in the morse and letter array...can you please assist me on that? Thanks!
if engmsg~=morse||engmsg~=letter%here
errordlg('You have entered an invalid figure','ERROR');
set(handles.morsecode_output,'string','INVALID');
else
Stephen23
on 23 Apr 2021
Edited: Stephen23
on 23 Apr 2021
"...if engmsg isn't equivalent to the items in the morse and letter array"
You need to define the required logic more precisely than that. For example, which of these should pass that test?:
engmsg = 'X..-'
engmsg = '..........'
engmsg = ''
Does "equivalent" include partial matches? Or do you only want to match whole words?
Phoebe
on 23 Apr 2021
Edited: Phoebe
on 23 Apr 2021
only the whole words I guess...for instance anything other than below will not be acceptted...how should I write the code?
%case 1
morse = {'-----','.----','..---','...--','....-','.....','-....','--...','---..','----.','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
%case 2
letter = ['0':'9','A':'Z','_'];
eg :if the user enters -1,!,*,& <--these will be error
Walter Roberson
on 23 Apr 2021
You have
[X,Y] = ismember(morsemsg,morse);
Any place that X is false, is a place that did not match any entry in morse
Stephen23
on 23 Apr 2021
Edited: Stephen23
on 23 Apr 2021
"like this"
No. As Walter Roberson pointed out, by using ISMEMBER you have already compared the content of those two variables, so you do not really need to compare them again. You can simply use the outputs from ISMEMBER together with ALL, ANY, and NOT as required. Start by looking at the inputs and outputs of these:
all(X)
any(X)
Look at your data, read the documentation, experiment!
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)