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

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

 Accepted Answer

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 Comments

LN has no value outside the function, im not entirely sure how to keep number in the previous column I rarely use arrays.
It must. It is the function output. Capture that, and pass it in as an argument to your function.
LN = [];
% generate values for when CT=1
CT = 1;
T = randi(10,1)
T = 4
LN = NumberBook(CT,T,LN)
LN = 1×10
4 0 0 0 0 0 0 0 0 0
% generate values for when CT=2
CT = 2;
T = randi(10,1)
T = 5
LN = NumberBook(CT,T,LN)
LN = 1×10
4 5 0 0 0 0 0 0 0 0
% generate values for when CT=3
CT = 3;
T = randi(10,1)
T = 1
LN = NumberBook(CT,T,LN)
LN = 1×10
4 5 1 0 0 0 0 0 0 0
function LN = NumberBook(CT,T,LN)
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
end
I moved it around a bit so that LN is defined outside the function and passed through;
if nargin < 3 || CT == 1 % Start of counter
LN = zeros(1,10); % Allocated space | 10 Digit restriction (Implimented later on)
end
LN = NumberBook(CT,T,LN); % Number Book
disp(LN)
Im still having the same issue when LN is passed through though;
function LN = NumberBook(CT,T,LN) % <- pass LN to the function
LN(CT) = T; % Adds number to array
end
By moving the if statement outside your function, naragin is no longer doing what you think it is doing, and what @Voss originally intended it to do.
Again, to provide an answer, we need to see the code that is calling NumberBook.
This is the function that Numberbook is called(I had to call numberbook as Numberbook(CT,T) without LN since LN doesnt exist outside numberbook if I use the function @Voss gave);
%% Output of each number
function y = Out(Num) % Calls Num to be used as a variable...
% Sets all Num characters to numerical values
% Does this all live
y = 0;
if strncmp(Num(1), '1', 1)
y = 1;
T = 1;
elseif strncmp(Num(1), '2', 1)
y = 2;
T = 2;
elseif strncmp(Num(1), '3', 1)
y = 3;
T = 3;
elseif strncmp(Num(1), '4', 1)
y = 4;
T = 4;
elseif strncmp(Num(1), '5', 1)
y = 5;
T = 5;
elseif strncmp(Num(1), '6', 1)
y = 6;
T = 6;
elseif strncmp(Num(1), '7', 1)
y = 7;
T = 7;
elseif strncmp(Num(1), '8', 1)
y = 8;
T = 8;
elseif strncmp(Num(1), '9', 1)
y = 9;
T = 9;
elseif strncmp(Num(1), '*', 1)
y = 10;
T = 10;
elseif strncmp(Num(1), '0', 1)
y = 11;
T = 0;
elseif strncmp(Num(1), '#', 1)
y = 12;
T = 11;
end
cnt = 1; % Keeps counter on
CT = Press(cnt); % Registers if button was pressed
set(groot,'defaultFigureVisible','off')
close(figure, 2)
Tone_Data(y); % Sets tone for each output
NumberBook(CT,T); % Number Book
end
The function Voss gave me;
function LN = NumberBook(CT,T,LN) % <- pass LN to the function
if nargin < 3 || CT == 1 % Start of counter
LN = zeros(1,10); % Allocated space | 10 Digit restriction (Implimented later on)
end
LN(CT) = T; % Adds number to array
disp(LN) % Displays updated number
end
It looks like you are trying to build a gui. How are you doing that? How is your function Out called? Is it called once each time a number is pressed, or is it called after the entire phone number has been entered?
The gui has its own function which adds the Number to a string aswell as defining Num as a character. Out is called with each button press utulizing the character that Num represents, turning it into a numerical value so that it can be used for equations/functions.
Then you will need to find a way to store each number in the gui. A function's workspace is cleared as soon as the function has executed. The easiest way is to add it to an Edit Field, which allows you to display the number to the user, too.
If you need to store it but not display it to the user, then you will need to save it to the figure.
  • For figure-based apps, this was traditionally done using guidata.
  • For uifigure based apps, you can use UserData.
If you really want to pursue a gui, I would suggest developing your app using App Designer instead.
Do you think you could give me an example of how I might store it using either of those functions for my particular use? I only need to store info for these values alone
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.
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)

Asked:

on 13 Nov 2023

Edited:

on 14 Nov 2023

Community Treasure Hunt

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

Start Hunting!