在Matlab中转换变量

时间:2017-10-16 17:48:21

标签: matlab

我是Matlab编程的新手。我有转换变量的问题。我必须将struct cell转换为double或int。我使用了一些函数(st2num,str2double等),这些函数没有用。如何将struct cell转换为int? (抱歉我的英语不好)

错误消息

convertting variable

prompt = 'Ogrenci sayisi: '; 
str = input(prompt,'s'); 
if isempty(str) 
    str = '0'; 
end 

for c = 1:str2num(str) 
    prompt = {'Ad:','Soyad:','Not:'}; 
    dlg_title = 'Input'; 
    num_lines = 1; 
    s(c).answer = inputdlg(prompt,dlg_title,num_lines); 
    if str2num(s(c).answer(3))>100 
        s(c).answer(3)=num2cell(0).i 
    end 
end

1 个答案:

答案 0 :(得分:0)

根据我的假设,'struct cell'@KendalUğurlu指的是转换结构中的单元阵列(例如,为了便于进一步计算)。这可以很容易地完成,例如,通过将结构转换为单元格数组:

s=struct;
mycell=struct2cell(s);

这是你的固定代码和注释(这对你很好),所以你可能理解为什么:

prompt = 'Ogrenci sayisi: '; %enter a quantity of student numbers to process
str = input(prompt,'s');
if isempty(str)
    str = '0';
end

for c = 1:str2num(str)
    prompt = {'Ad:','Soyad:','Not:'}; %ask more questions in a dialogue
    dlg_title = 'Input';
    num_lines = 1;
    s=struct; %define s as a structure, otherwise there will be errors
    s(c).answer = inputdlg(prompt,dlg_title,num_lines);
    if str2num(s(c).answer{3})>100 %if 'Not:' is larger than 100 do...
        s(c).answer(3)={'0'}; %enter 0 as string to keep datatypes within the cell array the same (and make it easier to convert later)
    end
end

s=struct2cell(s); %convert struct s to cellarray
s=s{:}'; %change cell within cell to just a cell, also changing dimensions (latter not really necessary)
s=cellfun(@(x) str2num(x),s) %convert cell array of numbers to a vector instead of cell array (not really necessary, but arguably easier to calculate with than strings within a cell array

那说 - 对我们好。为你的问题提供足够的背景(即'Ogrenci sayisi'意为'学生编号',来自for loop我猜这意味着'你想要处理多少学生数'',这有助于你避免陷入XY problem和我们提供实际解决您问题的答案。