Clear Filters
Clear Filters

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

12 views (last 30 days)
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)
LN(0+CT) = T; % Adds number to array
disp(LN) % Displays updated number
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

Accepted Answer

Cris LaPierre
Cris LaPierre on 13 Nov 2023
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.
Cris LaPierre
Cris LaPierre on 13 Nov 2023
Edited: Cris LaPierre on 14 Nov 2023
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 on 13 Nov 2023
Ill try using gui data for now, hopefully it serves the purpose i need it to.
Thank you

Sign in to comment.

More Answers (0)


Find more on Maintain or Transition figure-Based Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!