My code looks as so:
PosHotspot = dataset('file', 'PositiveHotspotpos.txt', 'Delimiter', '\t');
a = 2;
exon_end = PosHotspot.total_exon;
exonposition = PosHotspot.ExonPos;
Isoformnumber = PosHotspot.Isoform;
fileID = fopen('PosHotspot_results.txt', 'w')
for j = 1:660
exon = exonposition(j:j);
Isoform = Isoformnumber(j:j);
b = exon_end(j:j) - 1;
rng(0, 'twister');
r=randi([a b],1,1000);
less = sum(exon>r);
greater = sum(exon<r);
equal = sum(exon==r);
fprintf(fileID, '%s %4f %4f\n',Isoform,less,greater)
end
fclose(fileID)
However, I keep getting this error:
Error using fprintf
Function is not defined for 'cell' inputs.
Error in PositiveHotspotttest (line 24)
fprintf(fileID, '%s %4f %4f\n',Isofrom,less,greater)
I'm certain that it has to do with writing my information from Isoforms to the file.
Here's an example of what my file looks like:
chrom Gene Isoform exon_start ExonPos total_exon exonpos_exontotal
chr20 ADA NM_000022 43255096 4 13 0.307692307692
chr9 ALDOB NM_000035 104187734 7 10 0.7
chr5 ARSB NM_000046 78077674 7 9 0.777777777778
chr5 ARSB NM_000046 78135178 6 9 0.666666666667
chr5 ARSB NM_000046 78181406 5 9 0.555555555556
I want to output the Isoforms to my new file as well as the greater than and less than values. Is there a way to do this?
It's probably pretty simple, but again I'm new to matlab
Change:
Isoform = Isoformnumber(j:j);
to the more natural:
Isoform = Isoformnumber{j};
Like this you'll retrieve the content of the cell no. j, instead of the whole cell.
Related
I am trying to have a matrix which its elements doesn't have the same size .
Say the element_1 = 0.1234567 and the element_2 = 0.1 and I need the element_2 =0.1000000 so that both of them has the same size.
clc;clear all
a = rand(4,12);
COL_Names ={'This_is_Colu_No_1','This_is_Colu_No_2','This_is_Colu_No_3','This_is_Colu_No_4','This_is_Colu_No_5','This_is_Colu_No_6','This_is_Colu_No_7','This_is_Colu_No_8','This_is_Colu_No_9','This_is_Colu_No_10','This_is_Colu_No_11','This_is_Colu_No_12'};
rowNames = {'ROW1';'ROW2';'ROW3';'ROW4'};
T = array2table(a,'VariableNames',COL_Names,'RowNames',rowNames);
writetable(T,'Data.txt','Delimiter','\t','WriteRowNames',true);
type Data.txt ;
The OutPut is like this
Row This_is_Colu_No_1 This_is_Colu_No_2 This_is_Colu_No_3 This_is_Colu_No_4 This_is_Colu_No_5 This_is_Colu_No_6 This_is_Colu_No_7 This_is_Colu_No_8 This_is_Colu_No_9 This_is_Colu_No_10 This_is_Colu_No_11 This_is_Colu_No_12
ROW1 0.139740979774291 0.231035232035157 0.347778782863186 0.279682446566279 0.060995054119542 0.233212699943628 0.507599581908539 0.833087779293817 0.552819386888535 0.43251811668393 0.342580158122272 0.420574544492339
ROW2 0.00459708931895875 0.703626845695885 0.33064632159971 0.85782393462353 0 0.935097755896966 0.582441521353621 0.155241648807001 0.163717355897126 0.48985529896707 0.0134551766978835 0.810989133317225
ROW3 0.791254563282513 0.650747335567064 0.293769172888192 0.15110222627643 0.962791661993452 0.842147123142386 0.586462512126695 0.109349751268813 1 0.00525695361457879 0.700826048054212 0.989915984093474
ROW4 0.513993416249574 0.868158891144176 0.293769172888 0.552496163682282 0.301098948730568 0.779790450269442 0.420527994140777 0.523231514251179 0.0602548802340035 0.261436547849062 0.84923648156472 0.433189006269314
I think you need to do it manually using fprintf with formatSpec:
clc, clear, rng(3);
a = rand(4, 3);
colNames = {'This_is_Colu_No_9', 'This_is_Colu_No_10', 'This_is_Colu_No_11'};
rowNames = {'ROW98'; 'ROW99'; 'ROW100'; 'ROW101'};
formatSpecHead = '%-6s %-22s %-22s %-22s\n';
formatSpecRow = '%-6s %.20f %.20f %.20f\n';
fid = fopen('a.fwf', 'w');
fprintf(fid, formatSpecHead, 'Row', colNames{:}); % write header
for row = 1:size(a, 1)
fprintf(fid, formatSpecRow, rowNames{row}, a(row, :)); % write row
end
fclose(fid);
Then a.fwf looks like:
Row This_is_Colu_No_9 This_is_Colu_No_10 This_is_Colu_No_11
ROW98 0.55079790257457550418 0.89294695434765469777 0.05146720330082987793
ROW99 0.70814782261810482744 0.89629308893343806464 0.44080984365063646813
ROW100 0.29090473891294432729 0.12558531046383625274 0.02987621087856695556
ROW101 0.51082760519766301499 0.20724287813818675907 0.45683322439471107934
I am struggling with a text file that I have to read in. In this file, there are two types of line:
133 0102764447 44 11 54 0.4 0 0.89 0 0 8 0 0 7 Attribute_Name='xyz' Type='string' 02452387764447 884
134 0102256447 44 1 57 0.4 0 0.81 0 0 8 0 0 1 864
What I want to do here is to textscan all the lines and then try to determine the number of 'xyz' (and the total number of lines).
I tried to use:
fileID = fopen('test.txt','r') ;
data=textscan(fileID, %d %d %d %d %d %d %d %d %d %d %d %d %d %s %s %d %d','\n) ;
And then I will try to access data{i,16} to count how many are equal to Attribute_Name='xyz', it doesnt seem to be an efficient though.
what will be a proper way to read the data(what interests me is to count how many Attribute_Name='xyz' do I have)? Thanks
You could simply use count which is referenced here.
In your case you could use it in this way:
filetext = fileread("test.txt");
A = count(filetext , "xyz")
fileread will read the whole text file into a single string. Afterwards you can process that string using count which will return the occurrences from the given pattern.
An alternative when using older versions of MATLAB is this one. It will work with R2006a and above.
filetext = fileread("test.txt");
A = length(strfind(filetext, "xyz");
strfind will return an array which length represents the amount of occurrences of the specified string. The length of that array can be accessed by length.
There is the option of strsplit. You may do something like the following:
count = 0;
fid = fopen('test.txt','r');
while ~feof(fid)
line = fgetl(fid);
words = strsplit( line )
ind = find( strcmpi(words{:},'Attribute_Name=''xyz'''), 1); % Assume only one instance per line, remove 1 for more and correct the rest of the code
if ( ind > 0 ) then
count = count + 1;
end if
end
So at the end count will give you the number.
I am attempting to extract data from a DWT subband. I am able to embed data correctly (I have followed it in the debugger),cal PSNR etc. PSNR rate seem very high 76.2?? however,I am having lot of trouble extracting data back!It is sometimes extracting the number 128?? Can anyone help or have any idea why this is? I would be very thankful.I have been working on this all day & having no luck!I am very curious to know??
Data Embedding:
coverImage = imread('lena.bmp');
message = importdata('minutiaTest.txt');
%message = 'Bifurcations:';
[LL,LH,HL,HH] = dwt2(coverImage,'haar');
if size(message) > size(coverImage,1) * size(coverImage,2)
error ('message too big to embed');
end
bit_count = 0;
steg_coeffs = [4, 4.75, 5.5, 6.25, 7];
for jj=1:size(message,2)+1
if jj > size(message,2)
charbits = [0,0,0,0,0,0,0,0];
else
charbits = dec2bin(message(jj),8)';
charbits = charbits(:)'-'0';
end
for ii=1:8
bit_count = bit_count + 1;
if charbits(ii) == 1
if HH(bit_count) <= 0
HH(bit_count) = steg_coeffs(randi(numel(steg_coeffs)));
end
else
if HH(bit_count) >= 0
HH(bit_count) = -1 * steg_coeffs(randi(numel(steg_coeffs)));
end
end
end
end
stego_image = idwt2(LL,LH,HL,HH,'haar');
imwrite(uint8(stego_image),'newStego.bmp');
Data Extraction:
new_Stego = imread('newStego.bmp');
[LL,LH,HL,HH] = dwt2(new_Stego,'haar');
message = '';
msgbits = '';
for ii = 1:size(HH,1)*size(HH,2)
if HH(ii) > 0
msgbits = strcat (msgbits, '1');
elseif HH(ii) < 0
msgbits = strcat (msgbits, '0');
else
return;
end
if mod(ii,8) == 0
msgChar = bin2dec(msgbits);
if msgChar == 0
break;
end
msgChar = char (msgChar);
message = [message msgChar];
msgbits = '';
end
end
The problem arises from reading your data with importdata.
This command will load the data to an array. Since you have 39 lines and 2 columns (skipping any empty lines), its size will be 39 2. However, the program assumes that your message will be a string. For example, 'i am a string' has a size 1 13. This expectation of the program compared to the data you actually give it creates all sorts of problems.
What you want is to read your data as a single string, where the number 230 is not one element, but 3 individual characters. Tabs and newlines will also be read in as well.
To read your file:
message = fileread('minutiaTest.txt');
After you extract your message, to save it to a file:
fid = fopen('myFilename.txt','w');
fprintf(fid,message);
fclose(fid);
I want to make the results like this:
phraseblanks
phrasemat = Hello and how are you?
Hi there everyone!
How is it going?
WHazzup?
Phrase 1 had 4 blanks
Phrase 2 had 3 blanks
Phrase 3 had 2 blanks
Phrase 4 had 0 blanks
New phrasemat is :
Hello&and&how&are&you?
Hi&there&everyone!
How&is&it&going?
WHazzup?
so I made script "phraseblanks.m":
phrasemat = char('Hello and how are you?', ...
'Hi there everyone!', 'How is it going?', 'WHazzup?')
[r, c] = size(phrasemat);
for i = 1:r
phrasemat_new = cell(r, c);
howmany = countblanks(phrasemat(i, :));
fprintf('Phrase %d had %d blanks\n', i, howmany);
phrasemat(i,:) = strrep(phrasemat(i,:),' ','&')
phrasemat_new{i,:} = [phrasemat(i,:)];
end
fprintf('Changing one is %s\n', eval('phrasemat_new'));
script "countblanks.m":
function num = countblanks(phrase)
% countblanks returns the # of blanks in a trimmed string
% Format: countblanks(string)
num = length(strfind(strtrim(phrase), ' '));
end
and I keep having errors.
please help me..
I modified slightly your phraseblanks.m so that it works.
phrasemat = {'Hello and how are you?', ...
'Hi there everyone!', ...
'How is it going?', ...
'WHazzup?'};
r = numel(phrasemat);
phrasemat_new = cell(1, r);
for i = 1:r
howmany = countblanks(phrasemat{i});
fprintf('Phrase %d had %d blanks\n', i, howmany);
phrasemat{i} = strrep(phrasemat{i}, ' ', '&');
phrasemat_new(i) = phrasemat(i);
end
fprintf('Changing one is %s\n', phrasemat_new{:});
Obviously it could be written in a nicer, more "matlaby-way", but I didn't want to stride too far from your original version. Also you could consider using regular expressions, since if you have to spaces next to each other and you want to treat them as one blank space.
I have a large number of csv files to be processed. I only want the selected columns in each file and then load all the files from a certain folder and then output as one combined file. Here are my codes running with errors.... Could anyone help me to solve this problem?
data_directory = 'C:\Users\...\data';
numfiles = 17;
for n = 1:numfiles
filepath = [data_directory,'data_', num2str(n),'_output.csv'];
fid = fopen (filepath, 'rt');
wanted_columns= [2 3 4 5 10 11 12 13 14 15 16 17 35 36 41 42 44 45 59 61];
format = [];
columns = 109;
for i = 1 : columns;
if any (i == wanted_columns)
format = [format '%s'];
else
format = [format '%*s'];
end
end
data = textscan(fid, format, 'Delimiter',',','HeaderLines',1);
fclose(fid);
end
I think you should check whether the file is opened correctly. The error message seems to indicate that this is not the case. If it is not, check if the filepath is correct.
fid = fopen (filepath, 'rt');
if fid == -1
error('Failed to open file');
end
If the error is thrown here, you know that there was a problem with 'fopen'.
Ofcourse I don't know which files are on your computer, but I assume the '...' in the filename is not in your actual matlab file, only in your question on SO.
But could it be that you repeat the word 'data', while the actual filename only contains 'data' once? You code now will result in filenames like ''C:\Users\...\datadata_1_output.csv'. Maybe 'data' should be removed in data_directory or in filepath = ...?
Here is another way how you can setup the format string in a vectorized manner:
fcell = repmat({'%*s '},1,n_columns);
fcell(wanted_columns) = {'%s '};
formatstr = [fcell{:}];
Notice format is a build-in function in MATLAB, and it's better not to be used for variable name.