現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
how to insert fprintf value into GUI edit text box?
19 ビュー (過去 30 日間)
古いコメントを表示
Phoebe
2021 年 4 月 18 日
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 件のコメント
Stephen23
2021 年 4 月 19 日
"...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.
採用された回答
Stephen23
2021 年 4 月 18 日
編集済み: Stephen23
2021 年 4 月 18 日
24 件のコメント
Phoebe
2021 年 4 月 19 日
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
2021 年 4 月 19 日
編集済み: Stephen23
2021 年 4 月 19 日
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
2021 年 4 月 19 日
編集済み: Phoebe
2021 年 4 月 19 日
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
2021 年 4 月 19 日
"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
2021 年 4 月 19 日
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
2021 年 4 月 19 日
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
2021 年 4 月 19 日
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
2021 年 4 月 19 日
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
2021 年 4 月 19 日
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
2021 年 4 月 19 日
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
2021 年 4 月 23 日
編集済み: Phoebe
2021 年 4 月 23 日
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
2021 年 4 月 23 日
編集済み: Stephen23
2021 年 4 月 23 日
"...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
2021 年 4 月 23 日
編集済み: Phoebe
2021 年 4 月 23 日
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
2021 年 4 月 23 日
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
2021 年 4 月 23 日
編集済み: Stephen23
2021 年 4 月 23 日
"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!
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)