How to use genvarname variable after creation?

조회 수: 7 (최근 30일)
Lior Magen
Lior Magen 2021년 12월 13일
편집: Stephen23 2021년 12월 17일
let's take an ex. from here:
"Example 2
Read a column header hdr from worksheet trial2 in Excel® spreadsheet myproj_apr23:
[data hdr] = xlsread('myproj_apr23.xls', 'trial2');
Make a variable name from the text of the column header that will not conflict with other names:
v = genvarname(['Column ' hdr{1,3}]);
Assign data taken from the spreadsheet to the variable in the MATLAB workspace:
eval([v '= data(1:7, 3);']);"
How do I use the variable that is created?
  댓글 수: 1
Stephen23
Stephen23 2021년 12월 14일
편집: Stephen23 2021년 12월 17일
"How do I use the variable that is created?"
By using more slow, complex, inefficient code just like the code that you used to create that variable.
Is there a particular reason why you cannot use simpler and more efficient code (e.g. indexing, tables)?

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2021년 12월 14일
This is a terrible idea. You are forced to use eval every time.
The only time you wouldn't need eval is if you are using it to access fields of a struct. I would say that is the primary use case of this function.
  댓글 수: 3
Rik
Rik 2021년 12월 14일
I keep forgetting table exists. Maybe because I don't use them and they look to me like syntactic sugar for a struct vector. I suspect I'm just missing the point.
Steven Lord
Steven Lord 2021년 12월 17일
Storing the data from that sample table I created in a struct would require deciding which type of access to the data you're going to do more often and so want to make easier.
r = randperm(5).';
hocusPocus = r.^2;
% Scalar struct with array fields
s1 = struct('r', r, 'hocusPocus', hocusPocus);
% Or array of structs with scalar fields
s2 = struct('r', num2cell(r), 'hocusPocus', num2cell(hocusPocus));
It's easy to retrieve all the elements of one of the fields using s1 but more difficult to get a specific element from both r and hocusPocus.
allOfR = s1.r
allOfR = 5×1
3 5 1 4 2
secondElements.r = s1.r(2);
secondElements.hocusPocus = s1.hocusPocus(2)
secondElements = struct with fields:
r: 5 hocusPocus: 25
It's harder to get all of the elements of one of the fields using s2 but it's easier to get a specific element from both r and hocusPocus. The first one looks easy but that's because I took advantage of my knowledge that r contains scalars and so can be concatenated into a column vector. In the general case this is trickier.
allOfR = vertcat(s2.r)
allOfR = 5×1
3 5 1 4 2
secondElements = s2(2)
secondElements = struct with fields:
r: 5 hocusPocus: 25
In a table array, you can slice across rows or down variables basically equally easily.
t = table(r, hocusPocus);
allOfR = t.r % or
allOfR = 5×1
3 5 1 4 2
allOfR = t(:, 'r') % or t(:, 2)
allOfR = 5×1 table
r _ 3 5 1 4 2
secondElements = t(2, :)
secondElements = 1×2 table
r hocusPocus _ __________ 5 25

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by