How can I store and access strings in a cell array?

2 ビュー (過去 30 日間)
Dan Piraner
Dan Piraner 2012 年 10 月 20 日
I'm writing a program that requires dynamic storage and retrieval of strings. The logic flow looks something like this code:
initial_string = 'ABCDEF';
old_string = initial_string;
initial_pos = 1;
current_pos = 1;
previous_val = 1000;
best_string = cell(length(initial_string));
while current_pos < length(initial_string)
new_string = generate_new_string % generates new string of same length as
%initial string
[test_val, increment] = get_val(new_string, old_string);
if test_val < previous_val
best_string(current_pos) = new_string;
previous_val = test_val;
end
if increment == true
old_string = best_string{current_pos};
current_pos = current_pos + 1;
end
end
The problem is that get_val produces an error after increment becomes true because old string, rather being a string, is read as '[]'. How do I get old_string to become the string stored at best_string(current_pos) instead of the cell?
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 20 日
what is generate_new_string ?
Jan
Jan 2012 年 10 月 20 日
編集済み: Jan 2012 年 10 月 20 日
initial_string = 'ABCDEF';
best_string = cell(length(initial_string))
This creates a {6 x 6} cell. Is this wanted?
I do not understand the question. Which error does get_val produce? Perhaps you want:
best_string{current_pos} = new_string; % Curly braces

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by