how to insert fprintf value into GUI edit text box?

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 Kommentar

Stephen23
Stephen23 am 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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Stephen23
Stephen23 am 18 Apr. 2021
Bearbeitet: Stephen23 am 18 Apr. 2021

0 Stimmen

You need to use sprintf, not fprintf:
  • sprintf creates a character vector/string (this is what you need).
  • fprintf prints directly to a file or the command window.

24 Kommentare

Phoebe
Phoebe am 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
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
Phoebe am 19 Apr. 2021
Bearbeitet: Phoebe am 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
"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);
letter = ['1':'9', '0', 'A':'Z', '_'];
Phoebe
Phoebe am 19 Apr. 2021
I see, it works now thanks!!!
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','_'};
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
Phoebe am 19 Apr. 2021
I see... to both of you, thank you so much !!!
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);
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);
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
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
Phoebe am 20 Apr. 2021
I got it now thanks a lot! you're really helpful!
Phoebe
Phoebe am 23 Apr. 2021
Bearbeitet: Phoebe am 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
Stephen23 am 23 Apr. 2021
Bearbeitet: Stephen23 am 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
Phoebe am 23 Apr. 2021
Bearbeitet: Phoebe am 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
Perhaps you could just check for the expected characters:
rgx = '^(\w+|[-.]+)$';
isempty(regexp('ABC',rgx,'once'))
ans = logical
0
isempty(regexp('-..',rgx,'once'))
ans = logical
0
isempty(regexp('-23',rgx,'once'))
ans = logical
1
You have
[X,Y] = ismember(morsemsg,morse);
Any place that X is false, is a place that did not match any entry in morse
Phoebe
Phoebe am 23 Apr. 2021
Bearbeitet: Phoebe am 23 Apr. 2021
yea but how do i write that? because usually the given data are numbers so it's easier...this time it's in array form ..
Stephen23
Stephen23 am 23 Apr. 2021
Bearbeitet: Stephen23 am 23 Apr. 2021
Look at ANY and ALL and NOT.
Phoebe
Phoebe am 23 Apr. 2021
Bearbeitet: Phoebe am 23 Apr. 2021
like this? I'm getting errors sorry not really good at this
%below ismember...
if [X]=~morse||[X]=~letter
errordlg('You have entered an invalid figure','ERROR');
set(handles.morsecode_output,'string','INVALID');
else
Stephen23
Stephen23 am 23 Apr. 2021
Bearbeitet: Stephen23 am 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!
Phoebe
Phoebe am 23 Apr. 2021
I finally got it!!! thanks again !!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 18 Apr. 2021

Kommentiert:

am 23 Apr. 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by