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
"...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.
채택된 답변
댓글 수: 24
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.
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', '_'];
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
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.
I got it now thanks a lot! you're really helpful!
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
"...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?
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
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 ..
Look at ANY and ALL and NOT.
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
"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!
I finally got it!!! thanks again !!!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
