how a matlab .m code can write text in a GUI textarea?

6 ビュー (過去 30 日間)
Otavio Carpinteiro
Otavio Carpinteiro 2020 年 5 月 15 日
回答済み: Otavio Carpinteiro 2020 年 5 月 16 日
Let's suppose a simple file "test.m" with the following code:
obj = app1;
for i = 1:10
fprintf('\ni = %d\n', i);
if i==1
str = num2str(i);
else
str = sprintf('%s\n%s', str, num2str(i));
end
obj.writeText('1', str); % line 10
obj.TextArea_2.Value = str; % line 11
end
"app1.mlapp" is a GUI class made through appdesigner. In its code I defined two textareas:
% Create TextArea_1
app.TextArea_1 = uitextarea(app.UIFigure);
app.TextArea_1.Position = [47 187 259 150];
% Create TextArea_2
app.TextArea_2 = uitextarea(app.UIFigure);
app.TextArea_2.Position = [335 187 259 150];
and defined a public function:
methods (Access = public)
function writeText(app, ind, text)
if ind <= length(2)
ta = ['TextArea_', ind];
ta.Value = text;
end
end
end
only line 11 prints in in TextArea_2. So, why doesn't line 10 print in TextArea_1 ???
thanks in advance.
PS: in the attached file "GUI-example.zip", it's included the 2 files "app1.mlapp" and "test.m".
  4 件のコメント
Stephen23
Stephen23 2020 年 5 月 16 日
Putting pseudo-indices into variable names is a sign that you are doing something wrong.
Create one handle array:
app.TextArea(1) = uitextarea(app.UIFigure);
app.TextArea(2) = uitextarea(app.UIFigure);
and then use basic, simple, efficient indexing to access them, or even set / get for multiple objects/properties at once.
Otavio Carpinteiro
Otavio Carpinteiro 2020 年 5 月 16 日
> Putting pseudo-indices into variable names is a sign that you are doing something wrong.
certainly, but there was no other way. Handles are for functions, and what it's needed is to atribute one value to a variable "TextArea_X.Value", where X = 1, 2, ...
surely I can create a cell array of function handles, and call any function through its corresponding index of the cell array. Each function "func_X" changes the value of the variable "TextArea_X.Value". As my GUI should have around 50 textarea components, it would have 50 functions, one to write to its corresponding textarea component. Another possibility is to have just one function with a switch containing 50 clauses.
as this is the first time I program in matlab, I was expecting there would be a better way. Unfortunately, it seems there is not.
thank you for your reply. I'm closing this request ...

サインインしてコメントする。

採用された回答

Otavio Carpinteiro
Otavio Carpinteiro 2020 年 5 月 16 日
I'm closing this request, thanks.

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 5 月 16 日
length(2) is 1 and '1' < 1 is false, so you never execute the lines
ta = ['TextArea_', ind];
ta.Value = text;
If you did, then you get an error message, because ta would contain a character vector, and character vectors do not have a Value fieldname or property or method.
You should make app.Text_Area a vector that you index into.
  3 件のコメント
Walter Roberson
Walter Roberson 2020 年 5 月 16 日
You can have the app designer generate TextArea_1 and TextArea_2 for you, but your initialization can also do
app.TextArea = [app.TextArea_1, app.TextArea_2];
after which you can index app.TextArea(idx).Value
Otavio Carpinteiro
Otavio Carpinteiro 2020 年 5 月 16 日
as an array:
>> app.TextArea = [app.TextArea_1, app.TextArea_2]
Unable to resolve the name app.TextArea_1.
as a cell array:
>> app.TextArea = {app.TextArea_1, app.TextArea_2}
Unable to resolve the name app.TextArea_1.
as I reply to Stephen Cobeldick, I have two ways to workaround. I'll choose one of them.
thank you for your reply. I'm closing this request ...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by