필터 지우기
필터 지우기

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

조회 수: 5 (최근 30일)
Mason
Mason 2023년 11월 13일
편집: Cris LaPierre 2023년 11월 14일
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