How do you add values to an array, but keep the previous values

5 次查看(过去 30 天)
Im attempting to make a function that essentially just displays a 10-digit number to later sort that array through other values and verify the number exists in my file.
CT is a counter, It counts how many times a button was pressed, the value changes so that the next button "pressed" is placed into the column to the right of the previous number. T is the value of the number I want to input (This value is 0-9 depending on the button pressed). While this works, the previous values become 0 and the array is just a 1x10 of zeros with only the new value.
How might I fix this so that LN displays a 10 digit number using all my new values instead of just the most recent one.
function LN = NumberBook(CT,T)
if CT == 1 % Start of counter
LN = zeros(1,10); % Allocated space | 10 Digit restriction (Implimented later on)
end
LN(0+CT) = T; % Adds number to array
disp(LN) % Displays updated number
end
I essentially want it act like this per inputted value;
ans =
9 3 4 0 1 4 0 2 0 0
ans =
9 3 4 0 1 4 0 2 5 0
ans =
9 3 4 0 1 4 0 2 5 2
Instead of;
ans =
0 0 0 0 1 0 0 0 0 0
ans =
0 0 0 0 0 4 0 0 0 0
ans =
0 0 0 0 0 0 0 0 0 0

采纳的回答

Cris LaPierre
Cris LaPierre 2023-11-13
Is there more to your code? Keep in mind variable scope. Because you are inside a function, there are no previous values of LN.
Your solution of indexing into LN to make the assignment is the solution. However, you must provide an array with the previous values to your function.
  11 个评论
Cris LaPierre
Cris LaPierre 2023-11-13
编辑:Cris LaPierre 2023-11-14
I shared links to both doc pages. Each contains examples of how to use that approach to save values. Note that one approach is for figure-based apps, and one is for uifigure-based apps.
From your screenshot below, you have an edit box in your gui. Why not store the numbers there? Then you don't need to worry about saving it to the figure or displaying it in the command prompt. The app is where your user will see the number.
Mason
Mason 2023-11-13
Ill try using gui data for now, hopefully it serves the purpose i need it to.
Thank you

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Maintain or Transition figure-Based Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by