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
採用された回答
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 件のコメント
Mason
2023 年 11 月 13 日
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.
Cris LaPierre
2023 年 11 月 13 日
編集済み: Cris LaPierre
2023 年 11 月 13 日
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.
Mason
2023 年 11 月 13 日
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
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
2023 年 11 月 13 日
Ill try using gui data for now, hopefully it serves the purpose i need it to.
Thank you
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Environment and Settings についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
