In the documentations of sprintf and fprintf, in the list of special output format characters/operators, there is the '\a' character and they say it's "Alarm".
I tried printing it and it gives me an empty output.
>> sprintf('\a')
ans =
>>
What does it do?
It's the ASCII BEL (0x07) character:
>> sprintf('\a')+0
ans = 7
>> a = sprintf('\a')
a =
>> b = char(7)
b =
>> a==b
ans = 1
It's normally used to make the console beep, but it doesn't do anything in my Octave command window.
Related
It's easy to subset a character vector:
>> c = 'hello world';
>> c(1:5)
ans =
'hello'
But that doesn't work for strings:
>> s = "hello world";
>> s(1:5)
Index exceeds the number of array elements (1).
How do I subset a string?
You can use the extractBetween function:
>> s = "hello world";
>> extractBetween(s, 1, 5)
ans =
"hello"
This has to be done in Perl:
I have integers on the order of e.g. 30_146_890_129 and 17_181_116_691 and 21_478_705_663.
These are supposedly made up of 6 bytes, where:
bytes 0-1 : value a
bytes 2-3 : value b
bytes 4-5 : value c
I want to isolate what value a is. How can I do this in Perl?
I've tried using the >> operator:
perl -e '$a = 330971351478 >> 16; print "$a\n";'
5050222
perl -e '$a = 17181116691 >> 16; print "$a\n";'
262163
But these numbers are not on the order of what I am expecting, more like 0-1000.
Bonus if I can also get values b and c but I don't really need those.
Thanks!
number >> 16 returns number shifted by 16 bit and not the shifted bits as you seem to assume. To get the last 16 bit you might for example use number % 2**16 or number & 0xffff. To get to b and c you can just shift before getting the last 16 bits, i.e.
$a = $number & 0xffff;
$b = ($number >> 16) & 0xffff;
$c = ($number >> 32) & 0xffff;
If you have 6 bytes, you don't need to convert them to a number first. You can use one the following depending on the order of the bytes: (Uppercase represents the most significant byte.)
my ($num_c, $num_b, $num_a) = unpack('nnn', "\xCC\xcc\xBB\xbb\xAA\xaa");
my ($num_a, $num_b, $num_c) = unpack('nnn', "\xAA\xaa\xBB\xbb\xAA\xaa");
my ($num_c, $num_b, $num_a) = unpack('vvv', "\xcc\xCC\xbb\xBB\xaa\xAA");
my ($num_a, $num_b, $num_c) = unpack('vvv', "\xaa\xAA\xbb\xBB\xcc\xCC");
If you are indeed provided with a number 0xCCccBBbbAAaa), you can convert it to bytes then extract the numbers you want from it as follows:
my ($num_c, $num_b, $num_a) = unpack('xxnnn', pack('Q>', $num));
Alternatively, you could also use an arithmetic approach like you attempted.
my $num_a = $num & 0xFFFF;
my $num_b = ( $num >> 16 ) & 0xFFFF;
my $num_c = $num >> 32;
While the previous two solutions required a Perl built to use 64-bit integers, the following will work with any build of Perl:
my $num_a = $num % 2**16;
my $num_b = ( $num / 2**16 ) % 2**16;
my $num_c = int( $num / 2**32 );
Let's look at ( $num >> 16 ) & 0xFFFF in detail.
Original number: 0x0000CCccBBbbAAaa
After shifting: 0x00000000CCccBBbb
After masking: 0x000000000000BBbb
If we have
A=[100 -0.1 0];
B=[30 0.2 -2]; t1='text 1'; t2=text 2'
how to use fprintf so that the output saved in a file will look like that
100 -1.000E-0001 0.000E-0000 'text 1'
30 2.000E-0001 -2.000E-0000 'text 2'
I put together a "one-liner" (spread across several lines for better readability) that takes an array, a single number format, and a delimiter and returns the desired string. And while you found the leading blank-space flag, I prefer the + flag, though the function will work with both:
A=[-0.1 0];
B=[0.2 -2];
minLenExp = 4;
extsprintf = #(num,fmt,delim) ...
strjoin(cellfun(...
#(toks)[toks{1},repmat('0',1,max([0,minLenExp-length(toks{2})])),toks{2}],...
regexp(sprintf(fmt,num),'([+-\s][\.\d]+[eE][+-])(\d+)','tokens'),...
'UniformOutput',false),delim);
Astr = extsprintf(A,'%+.4E',' ');
Bstr = extsprintf(B,'%+.4E',' ');
disp([Astr;Bstr]);
Running this yields:
>> foo
-1.0000E-0001 +0.0000E+0000
+2.0000E-0001 -2.0000E+0000
(foo is just what the script file is called.)
Here's a more general approach that searches for the exponential format instead of assuming it:
A=[100 -0.1 0].';
B=[30 0.2 -2];
extsprintf = #(fmt,arr) ...
regexprep(...
sprintf(fmt,arr),...
regexprep(regexp(sprintf(fmt,arr),'([+-\s][\.\d]+[eE][+-]\d+)','match'),'([\+\.])','\\$1'),...
cellfun(#(match)...
cellfun(...
#(toks)[toks{1},repmat('0',1,max([0,minLenExp-length(toks{2})])),toks{2}],...
regexp(match,'([+-\s][\.\d]+[eE][+-])(\d+)','tokens'),...
'UniformOutput',false),...
regexp(sprintf(fmt,arr),'([+-\s][\.\d]+[eE][+-]\d+)','match')));
fmt = '%3d %+.4E %+.4e';
disp(extsprintf(fmt,A));
disp(extsprintf(fmt,B));
Outputs
>> foo
100 -1.0000E-0001 +0.0000e+0000
30 +2.0000E-0001 -2.0000e+0000
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.
Hey guys, I have a very simple problem in MATLAB:
I have some strings which are like this:
Pic001
Pic002
Pic003
004
Not every string starts with the prefix "Pic". So how can I cut off the part "pic" that only the numbers at the end shall remain to have an equal format for all my strings?
Greets, poeschlorn
If 'Pic' only ever occurs as a prefix in your strings and nowhere else within the strings then you could use STRREP to remove it like this:
>> x = {'Pic001'; 'Pic002'; 'Pic003'; '004'}
x =
'Pic001'
'Pic002'
'Pic003'
'004'
>> x = strrep(x, 'Pic', '')
x =
'001'
'002'
'003'
'004'
If 'Pic' can occur elsewhere in your strings and you only want to remove it when it occurs as a prefix then use STRNCMP to compare the first three characters of your strings:
>> x = {'Pic001'; 'Pic002'; 'Pic003'; '004'}
x =
'Pic001'
'Pic002'
'Pic003'
'004'
>> for ii = find(strncmp(x, 'Pic', 3))'
x{ii}(1:3) = [];
end
>> x
x =
'001'
'002'
'003'
'004'
strings = {'Pic001'; 'Pic002'; 'Pic003'; '004'};
numbers = regexp(strings, '(PIC)?(\d*)','match');
for cc = 1:length(numbers);
fprintf('%s\n', char(numbers{cc}));
end;