How can I delete the elements of array on MATLAB?

1 回表示 (過去 30 日間)
horizon
horizon 2019 年 5 月 31 日
コメント済み: Stephan 2019 年 5 月 31 日
The following code is executed but I have no idea to delete the elements of array on MATLAB.
And therefore, Array1 and Array2 are written out with the former data.
Every time when i is increased, I would like to reset Array1 and Array2.
How can I change the following code?
global Array1
global Array2
filename = 'data.xlsx';
for i = 1:3
sheet = i
for j = (1:2)-1
one = [1];
two = [2];
Array1 = [Array1; one]
Array2 = [Array2; two]
end
xlswrite(filename, Array1, sheet, 'A1');
xlswrite(filename, Array2, sheet, 'B1');
%here I would like to delete the elements of Array1 and Array2
end

採用された回答

Stephan
Stephan 2019 年 5 月 31 日
To delete an array use:
array1 = [];
  2 件のコメント
horizon
horizon 2019 年 5 月 31 日
Thank you for you answer.
Following your suggestion, is the below code right?
global Array1
global Array2
filename = 'data.xlsx';
for i = 1:3
sheet = i
for j = (1:2)-1
one = [1];
two = [2];
Array1 = [Array1; one]
Array2 = [Array2; two]
end
xlswrite(filename, Array1, sheet, 'A1');
xlswrite(filename, Array2, sheet, 'B1');
%here I would like to delete the elements of Array1 and Array2global
Array1 = [];
Array2 = [];
end
Stephan
Stephan 2019 年 5 月 31 日
That should work

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2019 年 5 月 31 日
global Array1
global Array2
filename = 'data.xlsx';
for i = 1:3
sheet = i
for j = (1:2)-1
one = [1];
two = [2];
Array1 = [Array1; one]
Array2 = [Array2; two]
end
xlswrite(filename, Array1, sheet, 'A1');
xlswrite(filename, Array2, sheet, 'B1');
clearvars Array1 Array2
end
  2 件のコメント
horizon
horizon 2019 年 5 月 31 日
Thank you for your answer.
When I executed your code, I've got the following error.
>> sample
sheet =
1
Array1 =
1
1
1
Array2 =
2
2
2
Array1 =
1
1
1
1
Array2 =
2
2
2
2
sheet =
2
Undefined function or variable 'Array1'.
Error in untitled (line 10)
Array1 = [Array1; one]
KSSV
KSSV 2019 年 5 月 31 日
Yes...that's true....you will get that error...try Stephan's solution..

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by