How to replace number with string in a matrix

I want to print like:
A =
1 1 1 1 1
1 1 1 1 1
1 1 Ali 1 1
1 1 1 1 1
1 1 1 1 1
but there are errors in my code:
for i = 1:5
for j = 1:5
A(i,j) = 1;
end
end
A

 採用された回答

Geoff Hayes
Geoff Hayes 2022 年 8 月 24 日
移動済み: Cris LaPierre 2022 年 8 月 24 日

1 投票

@Ali Raza - if you wish to combine numeric values with strings, then I think that you would need to use a cell array.

5 件のコメント

Cris LaPierre
Cris LaPierre 2022 年 8 月 24 日
Agreed. All data in a variable must be of the same data type. The exception is a cell array, but that is because each cell is treated as a separate variable.
Your other option is to make all values strings.
A = string(ones(5));
A(3,3) = "Ali"
A = 5×5 string array
"1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "Ali" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
Ali Raza
Ali Raza 2022 年 8 月 25 日
It works. Thank You so much
Lavanya
Lavanya 2022 年 9 月 13 日
Hello,
The below code is working in Matlab but I am unable to execute this same thing in Matlab app designer.
fig = figure('Interruptible', 'off');
ax1 = subplot(1,2,1);
ax2 = subplot(1,2,2);
fig.WindowButtonMotionFcn = @(~,~) [];
t = timer('Period', 0.1, 'ExecutionMode', 'fixedRate', 'TimerFcn', {@mouseMotionCB, ax1, ax2});
start(t)
ginput(1)
stop(t);
delete(t);
function mouseMotionCB(fig, event, ax1, ax2)
currentPoint1 = ax1.CurrentPoint(1,1:3);
x1 = currentPoint1(1);
y1 = currentPoint1(2);
if (ax1.XLim(1)<x1)&&(x1<ax1.XLim(2)) && (ax1.YLim(1)<y1)&&(y1<ax1.YLim(2))
fprintf('This is axes 1, Current Point is %f %f %f\n', currentPoint1);
end
currentPoint2 = ax2.CurrentPoint(1,1:3);
x2 = currentPoint2(1);
y2 = currentPoint2(2);
if (ax2.XLim(1)<x2)&&(x2<ax2.XLim(2)) && (ax2.YLim(1)<y2)&&(y2<ax2.YLim(2))
fprintf('This is axes 2, Current Point is %f %f %f\n', currentPoint2);
end
end
Can anyone help me with this?
Geoff Hayes
Geoff Hayes 2022 年 9 月 13 日
@Lavanya - I suggest that you remove this comment and create a new question as it doesn't seem to relate to the question posed by @Ali Raza. Also, has all of this code been put into your app, or just the relevant pieces? I would expect that your app (build from App Designer) would have the axes that you need to draw to...and so you wouldn't need the code to create a figure, add the subplots, etc....unless you want them to appear outside of the app.
Lavanya
Lavanya 2022 年 9 月 14 日
Here is the ''app1.mlapp'' file. This '.mlapp' file should work same as above the code. But its not working. Can you help me with this?

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

その他の回答 (0 件)

カテゴリ

タグ

質問済み:

2022 年 8 月 24 日

コメント済み:

2022 年 9 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by