Error: Cell contents reference from a non cell array object.
显示 更早的评论
I am using the following matlab code for selecting a particular row which matches a columnname from an access database. Once the row is retrieved , i try to store the values in a cell array .But i get an error when i try to print it.
q=FileNamevalue;
conn1=database('Dbname','','');
fna=exec(conn1,'select * from table1 where ImageName="',q,'"');
fna=fetch(fna);
fda=fna.data;
C = fda.'; %storing data in a cell array.
sprintf('ImageNo:%d\nImageName:%s\nDiseaseCategory: %s',C{1,1},C{2,1},C{3,1})
And here is the error: ??? Cell contents reference from a non-cell array object.
Error in ==> featurecomparison at 7 sprintf('IMAGENO:%d"\n"IMAGE NAME:%s"\n"DISEASE CATEGORY:%s"\n'); %Assumed to have 3 columns.
2 个评论
David Young
2014-3-2
Please format your code using the {}Code button.
Image Analyst
2014-3-2
And you forgot to post your error message. Post ALL the red text . Don't snip out just part of it or paraphrase it, give us the whole thing.
回答(3 个)
Image Analyst
2014-3-2
What is the class of fda? Is it a cell array? What does this show if you put it just before the line that throws an error
whos fda
class(fda)
My guess is that fda is not a cell and so C is not a cell either, and then when you try to do C{1,1} it gives you the error because in order to use braces C must be a cell, which it is not .
13 个评论
Monisha
2014-3-2
dpb
2014-3-2
Well, did you try it?
Why do you need it to be a cell and add the level of indirection anyway? Why not just use the (apparent) array directly?
Image Analyst
2014-3-2
I agree with dpb. We always try to avoid the complications and confusion of cells unless it's absolutely needed . By the way, why did you ignore my suggestions, or if you didn't, why did you not report back to us what it showed you?
Monisha
2014-3-3
Image Analyst
2014-3-3
Really bizarre. No line numbers or traceback? I'd definitely call the Mathworks on that because your error handler is not working correctly. Here's an example of what an error message might look like:
Undefined function or variable 'xclose'.
Error in test>calcmidpoints (line 31)
for n=1:length(xclose);
Error in test (line 8)
[midx,midy]=calcmidpoints(xclose,yclose);
See how it gives line numbers, etc.? In your code that you say the error is on, there are no braces so I don't see how that line can be the cause of a cell reference error.
Anyway, you're using sprintf and using %s in it but you're not supplying the things that are supposed to fill in for the %s. You need to pass in an integer and two strings just before the final ) of your sprintf call.
Well, you've not responded to the key point of what has been posted previously...
...
fname=fetch(fname,10);
fdata=fname.data;
%celldisp(fdata);
C = fdata.';
As previously asked, what's
whos fname fname.data
return? We've already (apparently) established that it's not a cell array and your assignment below doesn't turn it into one if it isn't. There's the whole nub of the problem but you're not really helping by not providing the requested information...
promptMsg=sprintf('IMAGENO:%d"\n ...
"IMAGE NAME:%s"\n ...
"DISEASE CATEGORY:%s"\n ...
"VIEW:%s"\n ...
"GRADE:%s"\n ...
"DESCRIPTION:%s" \n ...
"PROXIMITY:%s"\n ...
"SPECIALIZATION:%s"\n ...
"SPECIALIST:%s"\n ...
"TREATMENT:%s\n\n ...
Check the text file for further details ', C{:});
Now you've got a case where you need a numeric and 9(?) if I count correctly strings. Maybe it's a structure, not a cell array? But whatever, until we can see the details on that it's futile...
Monisha
2014-3-8
What the blazes is a "cursor" in this context? I presume you mean it's the returned object from the database query; we already know that from the code you previously posted. What we don't know is what form the data within that object is store--I'm thinking since we now know about the database that it's likely a structure with named fields but specifically we can't tell what those are to know how to address them (or even for absolutely certain that my supposition is correct).
ADDENDUM: See the Answer posted somewhat later after I read the documentation. The supposition of fname being an object is correct and I learned what a cursor object is and how to access it from that page.
Image Analyst
2014-3-8
dpb, I suggest you take the comments to your answer so you can get credit for it. I've about given up on this one since I can't run her program to help with debugging it whereas you still seem interested in helping.
I thought I'd give one more try for her to actually answer the question.
I'm guessing fetch -- well, I suppose ought to go to the help--oh, I see. There is a different answer but OP surely could have gotten to it herself one would think. I'll post a new answer.
OBTW, your question asking for the class was spot on...it turns out she's getting another cursor object created.
Monisha
2014-3-9
Image Analyst
2014-3-9
OK, well glad we could at least point you in the right direction.
dpb
2014-3-2
Taking a chance with the crystal orb...
C = fda.'; %storing data in a cell array.
Excepting it isn't--you forgot the curlies...
C = {fda.'};
in which case it appears you might just as well have written
C={fna.data};
dpb
2014-3-8
编辑:John Kelly
2015-2-27
...
fname=fetch(fname,10);
...
OK, from the database documentation for fetch one finds...
When fetch returns a cursor object, you can run many other functions, such as get and rows. To import data into the MATLAB workspace without metadata, use fetch with a database connection object as the input argument.
So, since we now know that fname is an object, we have to use the methods defined for that object.
See
and the links therefrom to get and joy should then ensue.
类别
在 帮助中心 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!