Convert workspace of cells to double
4 ビュー (過去 30 日間)
古いコメントを表示
Hi I have a workspace which is full of cell arrays I want a way which can change the entire workspace to double array.
rite now my workspace is small so i am able to use cell2mat(variable name). But there is a good chance it will increase in the future. Can anyone help me with it.
6 件のコメント
Stephen23
2018 年 1 月 10 日
編集済み: Stephen23
2018 年 1 月 10 日
"...rite now as I lack my knowledge in structures I can only think of using eval"
So you want to use eval... and you immediately get stuck on the trivial problem of accessing your data that you can't solve so you have to ask strangers to help you. Do you see the connection yet?
Accessing data is a trivial functionality, it should not cause problems like this. Importing data should be reliable, because the rest of your code depends on it. Why waste time messing around with eval or evalin when presumably actually processing your data is what you want to be doing?
"I am sure there is a much better way"
There are. Perhaps this would be a pertinent time to consider writing better (simpler, faster, neater, more efficient, much more reliable) code?
採用された回答
Guillaume
2018 年 1 月 10 日
We keep on saying that eval and co. are bad practice for good reasons. Yes, it may make it easy to create all these variables that you can easily see in your workspace with recognisable names but ultimately, it just make it harder to use these variables programmatically. As you've found out when you want to convert all of them. So really, don't do it! Ultimately, you'll find that the alternative are easier to use.
Changing your code to create a structure is easy. Replace the code in the (useless) ii loop by:
somestructname.(myheader{ii}) = mydata(:, 1);
However, assuming that you're using a fairly recent version of matlab all your problems (dynamic names, cell array, renaming) would mostly go away if you used readtable to read your excel files:
aliases = readtable('varnames.xlsx', 'Sheet', 'sheet1'); %import alias spreadsheet
aliases(ismissing(aliases.RealName), :) = []; %remove empty rows (if any)
aliases.importname = matlab.lang.makeValidName(aliases.AliasName); %actual variable name when imported
opts = detectImportOptions('dataset.xlsx');
[match, index] = ismember(aliases.importname, opts.VariableNames); %find real name of columns
opts.VariableNames(match) = aliases.RealName(index(match)); %replace column name
if ~all(match)
warning('Some columns do not have aliases: %s', strjoin(opts.VariableNames(~match), ', '));
end
opts.SelectedVariableNames = opts.VariableNames(match);
opts.VariableUnitsRange = 'A2'; %to import the unit row
dataset = readtable('dataset.xlsx', opts'); %automatically import with correct column name and type
8 件のコメント
Guillaume
2018 年 1 月 10 日
While we're on the topic of the code you wrote
RealParaIdx=find(cell2mat(cellfun(@(x)any(~isnan(x)),Realparam,'UniformOutput',false)));
This seems aimed at detecting empty rows. Why is there empty rows in the spreadsheet? Ignoring that, the cell2mat would be unnecessary if you hadn't ask cellfun to output a cell array, so:
RealParaIdx = find(cellfun(@(x)any(~isnan(x)),Realparam));
The find is also unnecessary. You can directly use the logical array returned by cellfun as a filter:
RealParaIdx = cellfun(@(x)any(~isnan(x)),Realparam)
AliasParaAvailable=AllAliasPara(RealParaIdx);
Realparanameavailable=Realparam(RealParaIdx);
Then, I don't understand why you write an excel file ( Realparanameavailable.xlsx) to read it back on the next line.
Then there is of course
for ii = ii
which is completely pointless.
Finally, If you're going to build string by concatenation and conversion, I'd recommend using sprintf:
commandExec = sprintf('%s = mydata(:, %d);', myheader{ii}, 1);
But really, I'd recommend using tables with a code similar to what I've written.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!