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

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?
@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