Creating a handle.object from a string

6 ビュー (過去 30 日間)
christopher bilham
christopher bilham 2017 年 12 月 8 日
編集済み: Stephen23 2019 年 8 月 18 日
I am trying to specify 1 of 19 axes in a GUI in a for loop. Depending on the data (randomly organized) it needs to be sorted to its appropriate axes. My sorting is working but I do not know how to reference the correct handle.axes in the loop. In my case they are axes(handle.tileN); N = 1:19.
axes(handles.tileN);
image(TEX)
axis off
axis image
I generate a string for each handle;
use = strcat('handle.tile',N)
This generates the correct string 'handle.tile1' if n=1; but I can not figure out how to then make this the correct handle. How do I make axes(handle.tileN); N = 1:19 correctly call objects?
  1 件のコメント
Stephen23
Stephen23 2019 年 8 月 18 日
編集済み: Stephen23 2019 年 8 月 18 日
The simplest and best solution is to change your code slightly, and use an array of handles:
H = gobjects(1,19); % or nan(...) for pre R2014b versions.
for k = 1:19
H(k) = axes(...)
end
and then you can trivially use indexing to access each axes.
"use = strcat('handle.tile',N) This generates the correct string 'handle.tile1' if n=1..."
I would be very surprised if that worked. It certainly doesn't when I try it:

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

回答 (1 件)

BK
BK 2019 年 8 月 18 日
編集済み: BK 2019 年 8 月 18 日
Likely there is a more elegant solution, but the following worked for me on a similar problem. Create a cell array where elements are handle objects (not the string names), then loop through as needed.
ax = {handles.axes1, handles.axes2,...};
for idx = 1:max_idx
imagesc(ax{idx},A);
ax{idx}.XAxis.Visible = 'off';
end
  1 件のコメント
Stephen23
Stephen23 2019 年 8 月 18 日
Rather than creating an extra level of complication by using a cell array, you can simply put the handles into one array:

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

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by