I've been at this for 3 hours -- so I need help.
I have a button on MATLAB's GUI GUIDE to load a text file to store 2
columns of data as x and y.
So x = [12, 12, 23];
textfile A is:
12 23
12 32
23 32
The code that is in the GUI GUIDE is under the pushbutton load_file as follows:
filename = uigetfile('*.txt')
loaddata = fullfile(pathname,filename)
load(loaddata)
A = filename(:,1)
B = filename(:,2)
handles.input1 = A;
handles.input2 = B;
axes(handles.axes1)
plot(handles.input1,handles,imput2)
load will load a text file, but it won't assign the contents to anything unless you explicitly specify an output.
%# load xy data from file
xy = load(loaddata,'-ascii')
%# assign columns to A and B, respectively
%# (why not x,y)?
A = xy(:,1)
B = xy(:,2)
The -ascii option of load is not necessary, but guarantees that the file is loaded as text, and will help you remember later that the data is supposed to be a text file.
Firstly, you might want to post your error message to make sure I'm reporting on the right problem, but I can see one problem right off:
the lines:
A = filename(:,1)
B = filename(:,2)
are only retrieving a string naming the file, not the actual data. So first, you have to know the name of the data that is being loaded, then change the load line to:
data = load(loaddata,'-ascii')
and now:
A = data(:,1)
B = data(:,2)
Related
I am currently working with a script that saves matrices as .mat files from other .mat files. I need to save 96 separate files so I have a loop that goes through the matrix names. I need to have the matrices saved with specific titles, that I have saved the titles in cell arrays {}. However, when I use the save(filename,variable) function, I get an error saying:
Error using save
Must be a text scalar.
Error in File_Creator (line 35)
save(name,fname);
My matrices need to be named 'PHI_Af','PHI_Am' (so on until) 'SLR_EF' (so every cr value needs to have a matrix with every par value. Here is what I am currently attempting:
cr = {'Af','Am','As','Aw','BS','BW','Cs','Cw','Cf','Ds','Dw','Df','ET','EF'};
par = {'PHI','BLD','KS','LAMBDA','PSIS','SLR'};
underscore = {'_'};
%% i and j are parameters in a loop where i = 1:length(par) and j = 1:length(cr)
%% f is the variable currently storing the matrix
s.(horzcat(par{i},underscore{1},cr{j})) = f;
name = string(strcat(par{i},'_',cr{j},'.mat'));
fname = string(s.(horzcat(par{i},underscore{1},cr{j})));
save(name,fname);
When I replace 'fname' with a generic string e.g. 'f', then the command runs but all the matrices save as the same thing ('f'), which makes it extremely difficult to run them all in the same script later.
I hope somebody can tell me what I'm doing wrong or provide me with a better solution. Please let me know if I can provide any more information.
Thank you
Assuming that the matrix, f, changes in each iteration of the loop (due to some other code you didn't post), it seems like this is all the code you need:
cr = {'Af','Am','As','Aw','BS','BW','Cs','Cw','Cf','Ds','Dw','Df','ET','EF'};
par = {'PHI','BLD','KS','LAMBDA','PSIS','SLR'};
for i = 1:length(par)
for j = 1:length(cr)
% add code here that loads the matrix f
name = [par{i}, '_', cr{j}, '.mat'];
save(name, 'f');
end
end
I have saved my job in Fortran with the following format
OPEN(50,file ='h.dat',form='formatted')
WRITE(50,'(101F12.6)')(u(k),k=1,nx)
CLOSE(50)
Since nx = 201, the data is saved in 2 lines. The first line has 101 columns, the second one has 100 columns. Therefore, MATLAB can not read h.dat with the following message “... must be the same as previous lines”.
Would it be possible to change this 2-line data to be 1-line data (201 columns) by using Matlab?
hh = importdata('h.dat');
size(hh) % ans = 2 101
nx = 201;
p = 0;
for i = 1:2;
for j = 1:101;
p = p+1;
ha(p) = hh(i,j);
end
end
ha = ha(1:nx);
save haa.dat ha -ascii
But, I think, it is much easier to use Fortran to solve it...
I want to save height and width value from multiple objects in one text file, but it only saved the last processed image data. Here's my code:
contents = get(hObject,'Value')
pilih=guidata(gcbo);
theImage{1}=getImage(handles.axes1);
theImage{2}=getImage(handles.axes2);
theImage{3}=getImage(handles.axes3);
theImage{4}=getImage(handles.axes4);
theImage{5}=getImage(handles.axes5);
for z = 1:5;
Img = theImage{z};
abu=rgb2gray(Img);
cb=imclearborder(abu);
thresh=graythresh(cb);
b=im2bw(cb,thresh);
bw=bwareaopen(b,60);
bwfill=imfill(bw,'holes');
s=regionprops(bwfill,'BoundingBox');
objects=cell(numel(s),1);
for idx=1:numel(s)
bb=floor(s(idx).BoundingBox);
out=bsxfun(#times,Img,uint8(bwfill));
objects{idx}=out(bb(2):bb(2)+bb(4),bb(1):bb(1)+bb(3),:);
end
X = zeros(3, numel(objects));
for k = 1:numel(objects)
k1=objects{k};
c1=rgb2gray(k1);
t1=graythresh(c1);
biner1=im2bw(c1,t1);
[height,width]=size(biner1);
a1=bwarea(biner1);
X(:,k)=[height;width;a1];
end
end
save grading/datauji.txt X -ascii;
What should I do? Thank you very much. Here's my images. There are 5 of them, and I want to save all of the objects height and width data in one txt file.
Try this code,
code
numel = 100;
file = fopen('ans.txt','w');
testdata = rand(3, numel);
X = zeros(3, numel);
for i=1:numel
X(:,i) = testdata(:,i);
fprintf(file,'%ld %ld %ld\n', X(1,i), X(2,i), X(3,i));
end
fclose(file);
I have created a dummy number of data points i.e 100. I have created random data in variable in 'testdata', then I have iterated 'numel' and every time saved saved all 3 data in comma separated type
ans.txt
11.0 2.2 3.1
4.2 5.2 1.1
32.1 542 12.1
12. 12 23
Hope this works.
I am creating a dummy example for my situation. Real problem is way more complicated.
Dummy Example:
I have a struct of size 2 with an entry called 'name' and an array of size 2x1.
So, the Struct called Store looks like:
Store(1).name = 'Apple';
Store(1).Data = [1; 2];
Store(2).name = 'Orange';
Store(2).Data = [24; 57];
I want to print this to excel such that it looks like the following:
Apple 1
Apple 2
Orange 24
Orange 57
I don't even know how to start with the above example. I have used xlswrite in the past but never for mixed type data.
If all your Data fields are of size 2x1, you should be able to the following:
% prepare cell to hold your data
xlData = cell(2*numel(store), 2);
% fill column 1:
xlData(1:2:end-1,1) = {store.name};
xlData(2:2:end,1) = xlData(1:2:end-1);
% fill column 2:
xlData(:,2) = num2cell(vertcat(store.Data));
% write to excel:
xlswrite('yourExcelfile.xlsx', xlData)
Got no Matlab at hand to test, but it should help you get going.
Since xlswrite takes a cell-array, there's no issue with mixed datatypes.
For some purposes, I wanna give an external text file as input of one of my MATLAB functions.
Generally this text file shows the following layout:
-----------------------------------------------------
HubHt = 90;
GridWidth = 220;
GridHeight = 220;
Ny = 35;
Nz = 37;
Nfft = 8192;
time = 620;
Uhub = 15;
Coherence = Bladed;
-----------------------------------------------------
To read it in, I'm currently calling this piece of code:
fid = fopen('test.inp','r+');
A = textscan(fid,'%s','Delimiter',';','commentStyle', '-','CollectOutput',1);
fclose(fid);
A = A{1};
inputs = regexp(A,' = ','split');
The last variable, inputs results in a <9x1> cell; each element will be a <1x2> cell.
The first element of the <1x2> cell is supposed to be the field of a overall INPUT structure, whereas the second element is the associated parameter.
At the moment, I'm using a quite static and awful way to achieve my goal:
inp = struct(char(inputs{1}(1)),str2double(inputs{1}(2)),char(inputs{2}(1)),str2double(inputs{2}(2)),char(inputs{3}(1)),str2double(inputs{3}(2)),char(inputs{4}(1)),str2double(inputs{4}(2)),char(inputs{5}(1)),str2double(inputs{5}(2)),char(inputs{6}(1)),str2double(inputs{6}(2)),char(inputs{7}(1)),str2double(inputs{7}(2)),char(inputs{8}(1)),str2double(inputs{8}(2)),char(inputs{9}(1)),char(inputs{9}(2)));
I believe that exist quite better ways to do the same: I'd like if you could share one with me.
You can use cell2struct:
% create cell vector where fieldnames and values alternate
tmp = [inputs{:}];
inp = cell2struct(tmp(2:2:end), tmp (1:2:end), 2);
Since what you have written is (nearly) valid Matlab source code why not give it the file extension .m and just run it ? Or call it from inside your function.
This is is an approach which we've used a lot; it's straightforward and simple. Obviously you have to make sure that it is (entirely) valid Matlab source but that's not difficult.