英文:
Scilab execstr csvRead in the loop
问题
I tested following function in Scilab 6.1 and 2023.1. I try to read all *.txt
files in a folder one by one and load it to a matrix.
datfiles=ls('*.txt','r');// List all *.txt Files in datfiles
for i = 1:size(datfiles, 1) // for each *.txt file
token=strtok(datfiles(i,:),'.'); // save name of each txt.file and...
// execstr(token + '=csvRead('+ string(datfiles(i,:)) + ', ascii(9), [], ''string'')'); // use the name of the .txt file as variable name, where as all data is saved.
execstr('csvRead('+ string("StepPo135.txt")+')'); //
end
I still get following error. By the way. How can I change this output to English? My preferences are set to English.
In line 1 of executed string
Attempt to reference a field in a non-structured array.
Error: Try, reference one field in not a structured array
英文:
I tested following function in Scilab 6.1 and 2023.1. I try to read all *.txt
files in a folder one by one and load it to a matrix.
datfiles=ls('*.txt','r');// List all *.txt Files in datfiles
for i = 1:size(datfiles, 1) // for each *.txt file
token=strtok(datfiles(i,:),'.'); // save name of each txt.file and...
// execstr(token + '=csvRead('+ string(datfiles(i,:)) + ', ascii(9), [], ''string'')'); // use the name of the .txt file as variable name, where as all data is saved.
execstr('csvRead('+ string("StepPo135.txt")+')'); //
end
I still get following error. By the way. How can I change this output to english? My preferences are set to english
*bei Zeile 1 von ausgeführtem String
Versuch, ein Feld in einem nicht strukturierten Array zu referenzieren.*
Error: Try, reference one field in not a stuctured array
答案1
得分: 1
这是解决方案:
datfiles = ls('*.txt'); // 列出所有 *.txt 文件在 datfiles 中
for i = 1:size(datfiles, 1) // 对于每个 *.txt 文件
token = strtok(datfiles(i,:), '.'); // 保存每个 txt 文件的名称并...
execstr(token + "=csvRead(" + "\"" + datfiles(i,:) + "\"" + ", ascii(9), [], 'string')");
end
英文:
This is the solution:
datfiles=ls('*.txt');// List all *.txt Files in datfiles
for i = 1:size(datfiles, 1) // for each *.txt file
token=strtok(datfiles(i,:),'.'); // save name of each txt.file and...
execstr(token+"=csvRead("""+datfiles(i,:)+""", ascii(9), [], ''string'')");
end
答案2
得分: 0
你需要通过加倍引号或双引号来添加引号,就像这样
execstr("token=csvRead("""+datfile+""")")```
错误消息是因为Scilab试图解释`StepPo135.txt`而没有引号,也就是说,它尝试提取`StepPo135`作为结构体的`txt`字段。
<details>
<summary>英文:</summary>
You need to add quotes or double quotes by doubling them, like this
datfile="StepPo135.txt";
execstr("token=csvRead("""+datfile+""")")
The error message is due to Scilab trying to interpret `StepPo135.txt` without the quotes, i.e. as if you were trying to extract the `txt` field of `StepPo135` considered as a struct.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论