【发布时间】:2020-11-09 16:17:51
【问题描述】:
我正在编写一个显示字符数组的脚本(我会使用字符串数组,但 inputdlg 需要 char),允许用户编辑数组,并将新数组输出到文本文件中。
但是,我遇到了无法将输出 (vals1) 格式化为文本文件的问题。我认为部分问题在于 inputdlg 命令输出了一个 1x1 数组,很难将其转换回我开始使用的逐行格式(在本例中为 arr)。
下面的代码输出按列而不是按行读取的单行:“A1ReB2otC3bcD4e E5re 6tt 7 c 8S 9m i th h”。我不确定如何转换它,因为 charvals1(inputdlg 输出)返回相同的字符串。
有没有办法在用户输入新数组后返回逐行输出(而不是 1x1 数组字符串),或者打印重新格式化的 inputdlg 输出(包括换行符)?
arr = char(["ABCDE";
"123456789";
"Robert Smith";
"etc etc"])
% User updates the array
prompt = {'Update content below if necessary'};
dlgtitle = "Section 2";
dims = [30 50];
definput = {arr};
charvals1 = inputdlg(prompt,dlgtitle,dims,definput);
vals1 = convertCharsToStrings(charvals1);
% Outputting the updated array to text file
prompt = {'Enter desired input file name'};
dlgtitle = "Input Name";
dims = [1 35];
definput = {'Input Name'};
fileName = inputdlg(prompt,dlgtitle,dims,definput);
selected_dir = uigetdir();
fileLocation = char(strcat(selected_dir, '\', string(fileName(1)),'.txt'));
txtfile = fopen(fileLocation,'wt');
fprintf(txtfile, '%s\n', vals1) ;
【问题讨论】: